list 将 lapply 的输出提取到数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10590904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:50:43  来源:igfitidea点击:

Extracting outputs from lapply to a dataframe

rlistdataframe

提问by robintw

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

我有一些 R 代码,它使用以下代码对当前目录中的所有文件执行一些数据提取操作:

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:

lapply 的输出如下(使用 提取dput())——基本上是一个充满向量的列表:

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.2etc).

不过,我想,然后用此输出与两列的数据帧处理:一个是字母代码("amer""appl"等等),一个用于数字(14.514.2等)。

Unfortunately, as.data.framedoesn'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_timereturns 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?

不幸的是,as.data.frame似乎不适用于列表中嵌套向量的这种输入。我应该如何转换这个?我是否需要更改函数data_for_time返回值的方式?目前它刚刚返回c(name, value)。或者有没有一种很好的方法可以将这种输出转换为数据帧?

采纳答案by joran

One option might be to use the ldplyfunction from the plyrpackage, which will stitch things back into a data frame for you.

一种选择可能是使用plyr包中的ldply函数,它将为您将事物拼接回数据框中。

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 willwant 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).

但请注意,如果你是混合变量类型有c(),你可能想改变你的函数简单地返回data.frame(name= name,value = value)代替c(name,value)。否则一切都会被强制转换为字符(就像我上面的例子一样)。

回答by fotNelton

Try this if resultswere your list:

如果results是你的清单,试试这个:

> 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
...

回答by IRTFM

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

回答by Tyler Rinker

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:

因为,对于Nelton 采取了我正在给予的回应,而Joran 接受了我能想到的唯一其他合理的回应,而且由于我应该写一篇论文,这里有一个荒谬的答案:

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