robintw
robintw

Reputation: 28591

Extracting outputs from lapply to a dataframe

I have some R code which performs some data extraction operation on all files in the current directory, using the following code:

files <- list.files(".", pattern="*.tts")
results <- lapply(files, data_for_time, "17/06/2006 12:00:00")

The output from lapply is the following (extracted using dput()) - basically a list full of vectors:

list(c("amer", "14.5"), c("appl", "14.2"), c("brec", "13.1"), 
c("camb", "13.5"), c("camo", "30.1"), c("cari", "13.8"), 
c("chio", "21.1"), c("dung", "9.4"), c("east", "11.8"), c("exmo", 
"12.1"), c("farb", "14.7"), c("hard", "15.6"), c("herm", 
"24.3"), c("hero", "13.3"), c("hert", "11.8"), c("hung", 
"26"), c("lizr", "14"), c("maid", "30.4"), c("mart", "8.8"
), c("newb", "14.7"), c("newl", "14.3"), c("oxfr", "13.9"
), c("padt", "10.3"), c("pbil", "13.6"), c("pmtg", "11.1"
), c("pmth", "11.7"), c("pool", "14.6"), c("prae", "11.9"
), c("ral2", "12.2"), c("sano", "15.3"), c("scil", "36.2"
), c("sham", "12.9"), c("stra", "30.9"), c("stro", "14.7"
), c("taut", "13.7"), c("tedd", "22.3"), c("wari", "12.7"
), c("weiw", "13.6"), c("weyb", "8.4"))

However, I would like to then deal with this output as a dataframe with two columns: one for the alphabetic code ("amer", "appl" etc) and one for the number (14.5, 14.2 etc).

Unfortunately, as.data.frame doesn't seem to work with this input of nested vectors inside a list. How should I go about converting this? Do I need to change the way that my function data_for_time returns its values? At the moment it just returns c(name, value). Or is there a nice way to convert from this sort of output to a dataframe?

Upvotes: 21

Views: 44956

Answers (4)

joran
joran

Reputation: 173697

One option might be to use the ldply function from the plyr package, which will stitch things back into a data frame for you.

A trivial example of it's use:

ldply(1:10,.fun = function(x){c(runif(1),"a")})
                    V1 V2
1    0.406373084755614  a
2    0.456838687881827  a
3    0.681300171650946  a
4    0.294320539338514  a
5    0.811559669673443  a
6    0.340881009353325  a
7    0.134072444401681  a
8  0.00850683846510947  a
9    0.326008745934814  a
10    0.90791508089751  a

But note that if you're mixing variable types with c(), you probably will want to alter your function to return simply data.frame(name= name,value = value) instead of c(name,value). Otherwise everything will be coerced to character (as it is in my example above).

Upvotes: 16

IRTFM
IRTFM

Reputation: 263471

inp <- list(c("amer", "14.5"), c("appl", "14.2"), .... # did not see need to copy all

data.frame( first= sapply( inp, "[", 1), 
            second =as.numeric( sapply( inp, "[", 2) ) )

   first second
1   amer   14.5
2   appl   14.2
3   brec   13.1
4   camb   13.5
5   camo   30.1
6   cari   13.8
snipped output

Upvotes: 3

Tyler Rinker
Tyler Rinker

Reputation: 110034

Because and forNelton took the response I was in the process of giving and Joran took the only other reasonable response I could think of and since I'm supposed to be writing a paper here's a ridiculous answer:

#I named your list LIST
LIST2 <-  LIST[[1]]
lapply(2:length(LIST), function(i) {LIST2 <<- rbind(LIST2, LIST[[i]])})
data.frame(LIST2)

Upvotes: 2

fotNelton
fotNelton

Reputation: 3894

Try this if results were your list:

> as.data.frame(do.call(rbind, results))

     V1   V2
1  amer 14.5
2  appl 14.2
3  brec 13.1
4  camb 13.5
...

Upvotes: 56

Related Questions