从lapply提取输出到数据框 [英] Extracting outputs from lapply to a dataframe
问题描述
我有一些R代码,使用以下代码对当前目录中的所有文件执行某些数据提取操作:
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")
lapply的输出如下(使用dput()
提取)-基本上是一个充满向量的列表:
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"))
但是,我想将此输出作为具有两列的数据帧处理:一列用于字母代码("amer"
,"appl"
等),一列用于数字(14.5
,14.2
等) ).
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).
不幸的是,as.data.frame
似乎不适用于列表中嵌套向量的这种输入.我应该如何转换呢?我是否需要更改函数data_for_time
返回其值的方式?此刻,它仅返回c(name, value)
.还是有一种很好的方法将这种输出转换为数据框?
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?
推荐答案
一个选项可能是使用 plyr 包中的ldply
函数,该函数会将内容缝合回一个数据框中以供你.
One option might be to use the ldply
function from the plyr package, which will stitch things back into a data frame for you.
一个简单的用法示例:
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
但是请注意,如果要将变量类型与c()
混合,则 可能希望更改函数以仅返回data.frame(name= name,value = value)
而不是c(name,value)
.否则,所有内容都会被强制转换为字符(就像上面的示例中一样).
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).
这篇关于从lapply提取输出到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!