2

I am trying to create a data frame containing multiple cor.test results.

I have used the apply method to generate a list of multiple results for my data set:

tests<-apply(data[,-1],2,cor.test,data$somevariable)

I've then tried to turn that list into a data frame using do.call and rbind:

df<-do.call(rbind,tests)

Here is the structure of the data after this point:

structure(list(c(t = 9.73527458723168), c(t = 11.6358332145994), 
    c(t = -8.41342945363561), c(t = 4.76930273088914), c(t = 5.38274340720866), 
    c(t = 1.36141073255796), 2.0305145351559e-19, 1.10699280065747e-25, 
    2.25755786735353e-15, 3.01264916719032e-06, 1.58134326207236e-07, 
    0.174506476001015, structure(c(0.41422989714479, 0.5904698126317
    ), conf.level = 0.95), structure(c(0.490934145176199, 0.649810665047493
    ), conf.level = 0.95), structure(c(-0.542907553904067, -0.354506442300791
    ), conf.level = 0.95), structure(c(0.164439228019406, 0.383047613908603
    ), conf.level = 0.95), structure(c(0.198753649224564, 0.412910725931687
    ), conf.level = 0.95), structure(c(-0.0365206469491267, 0.198474167071758
    ), conf.level = 0.95)), .Dim = c(6L, 3L), .Dimnames = list(
    c("hour", "temp_C", "humidity", "wind_speed_ms", "visibility_10m", 
    "dew_point_temp_C"), c("statistic", "p.value", "conf.int"
    )))

The result is almost what I am after. However, there are two problems:

1.) the return is a list as opposed to a data frame. Using as.data.frame(df) returns a data frame in which all values appear replaced with their type i.e. <dbl[1]>.

2.) Values for conf.int column seem to be condensed to numeric,2 as opposed to displaying both values.

I feel I am a step away from where I need to be here. I have tried to use bind_rows(df,.id='column_label') as a final step but receive the error "Argument 17 must have names"

Any help on getting the correct dataframe of cor.test results would be much appreciated.

r0bt
  • 383
  • 3
  • 12
  • Please add an example of your data.frame, e.g. using `dput`, to create a reproducible example. – JKupzig Jan 12 '22 at 10:38
  • Yeah, looks you have a tibble instead of a data frame. Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example before posting in R tag, thanks. – jay.sf Jan 12 '22 at 10:41
  • structure added – r0bt Jan 12 '22 at 11:00

1 Answers1

1

I tried your code, and it seems to work if we replace as.data.frame(df) with data.frame(df).

Since I do not have your data, I used the Auto data set from the ISLR package (see Introduction to Statistical Learning, James et al., 2013).

library(ISLR)
data = Auto

tests = apply(data[, -9], 2, cor.test, data$weight) # Removing data$name -> It is a factor!

df = do.call(rbind, tests)
new.df = data.frame(df) # Using data.frame()!

new.df should be the result you are looking for.

About the differences between as.data.frame() and data.frame(), a quick search on Google leads to this link .

riccardo-df
  • 512
  • 4
  • 9