R创建数据框列表的数据框的新列表 [英] R create new list of dataframes of a list of dataframes

查看:102
本文介绍了R创建数据框列表的数据框的新列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框列表(列表名为result),现在我想创建一个新的数据框列表,但不知何故我无法弄清楚。
我尝试了两个版本,一个使用循环,一个使用lapply。在这种特殊情况下,我想提取一些列(具有特定名称)并将其存储在一个新的数据框列表中(称为l)

I have a list of dataframes (list named result) and now I want to create a new list of dataframes but somehow I cannot figure this out. I tried two versions, one with a loop and one with lapply. In this particular case I want to extract some columns (with a specific name) and store it in a new list of dataframes (called l)

l<- lapply (results, function (i)results[[i]][, c("c1","c2")])

for (c in seq_along(results)){
  print(l[i]<-results[[i]][, c("c1","c2")])
}

第一个函数告诉我这对于类型列表来说是无效的。
第二个功能只打印它,但这不允许进一步的分析。如果我不使用打印,只有最后一个选项被评估。
理想情况下,我想在这个循环中用l数据框做一些事情,例如熔解l和添加列。
你有什么提示我误解了吗?
非常感谢!

The first function tells me that it is invalid for type list to do this. The second function only prints it but this doesnt allow further analyses. If I do not use print, only the last option gets evaluated. Ideally, I want to do several things with the "l" dataframes in this loop, e.g., melting l and adding columns. Do you have any hints what I have been misunderstanding? Thanks a lot!!

推荐答案

可能应该是:

It probably should be:

l<- lapply (results, function (df) df[  c("c1","c2") ])

如果您想使用序列号进行索引,它也可以是

If you wanted to index with the sequence number, it could also be

l<- lapply ( seq_along(results) , function (i)results[[i]][, c("c1","c2")])

这篇关于R创建数据框列表的数据框的新列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆