Search code examples
rdataframeanalysis

Treating values in a list of data frames as one object in r


I am trying to apply various analysis functions (lm, plot, etc.) to a long list of data frames. All of the data frames have the same column headings, and I want to calculate 1 result for all data under each column heading in the data frame. I can't figure out how to use the 'unlist' function in order to do this.

IE for the example below, I would want one plot with n = 15, and one linear model of all $a ~ all $b.

d1 <- data.frame(a=rnorm(5), b=1:5, c=rnorm(5))
d2 <- data.frame(a=1:5, b=rnorm(5), c =rnorm(5))
d3 <- data.frame(a=1:5, b=rnorm(5), c= rnorm(1:5))

my_test_data <- list()

my_test_data <- list(d1, d2, d3)

I have tried everything, including:

plot(unlist(my_test_data), my_test_data$a)

lm(my_test_data$a ~ my_test_data$b, unlist(my_test_data))

Let me know what I am missing - all help greatly appreciated.


Solution

  • You could use rbind instead to combine the data.frames?

    d1 <- data.frame(a=rnorm(5), b=1:5, c=rnorm(5))
    d2 <- data.frame(a=1:5, b=rnorm(5), c =rnorm(5))
    d3 <- data.frame(a=1:5, b=rnorm(5), c= rnorm(1:5))
    
    my_test_data <- rbind(d1, d2, d3)
    
    m <- lm(a ~ b, data = my_test_data)
    plot(a ~ b, data = my_test_data)
    abline(m)