English 中文(简体)
将产出从应用到数据框架
原标题:Extracting outputs from lapply to a dataframe

我有某些R代码,使用以下代码对目前名录的所有档案进行某些数据提取操作:

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

配对的产出如下(使用<代码>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"))

然而,我随后将这一产出作为两个栏目处理:一栏是字母代码(amer”appl>等)和一栏(14.514.2等)。

遗憾的是,as.data.frame 在清单中,似乎没有与这种内载病媒的投入合作。 我应如何改变这种状况? 我是否需要改变我的职能<代码>数据_for_time? 此时此刻,它只是<代码>c(名称、价值)。 或者,从这种产出转换到数据框架还有很多办法?

最佳回答

一种选择是使用<代码>ldply功能,从plyr/strong>包裹中转,这将把事情推回到你的数据框架中。

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

但请注意,如果你将各种类型与<代码>c(>)重新组合起来,你很可能要 /will,希望改变您的职能,只将<代码>(姓名=名称、价值 = 数值)而不是<编码>c(名称、价值)。 否则,所有事情都将受到胁迫(如上例)。

问题回答

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

由于恩埃尔顿和恩特尔顿都作出了回应,我正在考虑,若兰采取其他唯一合理的对策,而且由于我本应在此撰写一份论文,我可以 answer地回答:

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




相关问题
Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Is List<> better than DataSet for UI Layer in ASP.Net?

I want to get data from my data access layer into my business layer, then prepare it for use in my UI. So i wonder: is it better to read my data by DataReader and use it to fill a List<BLClasses&...

What is the benefit to using List<T> over IEnumerable<T>?

or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at ...

灵活性:在滚动之前显示错误的清单

我有一份清单,在你滚动之前没有显示任何物品,然后这些物品就显示。 是否有任何人知道如何解决这一问题? 我尝试了叫人名单。

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...