0

I've previously asked this question: How to add the results of applying a function to an existing data frame?

I thought I could use that as a template and unnest() the results of my oddsratio test to the dataframe.

Here's some numbers

thing<-matrix(c(33,2153,48,2528,1577,30335,66,1916,24,1162,15,910),nrow=6,byrow=T)
colnames(thing)<-c("SM","AE")
rownames(thing)<-c("a","b","c","d","e","f")
oddsratio(thing)

I was hoping that if I did this:

oddsthing<-as.data.frame(thing)
oddsthing<-oddsthing%>%mutate(res=list(oddsratio(thing)))%>%unnest()

But it does not produce the output I was hoping for.

When I write

thing_list<-oddsratio(thing)

I can see four elements in the list: data, measure, p.value, correction

How do I get the measure and the pvalue from the list appended as columns to the right of the SM AE columns?

How do I access the things in the list? I can unnest($res) but that doesnt work either.

I have a feeling I'm almost there....

thnaks!

damo
  • 463
  • 4
  • 14

1 Answers1

2

Generally, you can use View(thing_list) to see the "structure" of the list or here you can use names(thing_list) which shows you "data" "measure" "p.value" "correction". Once you know that you can you cbind.data.frame to add the desired columns to you dataframe like that:

thing <- cbind.data.frame(thing, thing_list$measure, thing_list$p.value)
thing
    SM    AE  estimate     lower     upper  midp.exact              fisher.exact            chi.square
a   33  2153 1.0000000        NA        NA          NA                        NA                    NA
b   48  2528 0.8084932 0.5123434 1.2605062 0.350119674 0.36987910540283064353417 0.3468330287919010879
c 1577 30335 0.2962728 0.2051051 0.4121023 0.000000000 0.00000000000000007500751 0.0000000000002497838
d   66  1916 0.4461373 0.2888623 0.6758792 0.000115398 0.00014013356896922285933 0.0001160955897881125
e   24  1162 0.7408147 0.4366285 1.2754370 0.274390974 0.26665380954122580581256 0.2689008535316412263
f   15   910 0.9244444 0.5073984 1.7659563 0.804430890 0.87366076007998338948113 0.8167701545656785855