R-从名称列表中循环遍历多个数据框 [英] R - loop through multiple dataframes from a list of names

查看:171
本文介绍了R-从名称列表中循环遍历多个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多大型DF,我想遍历它们.与其将它们绑定在一起以形成一个大列表,我想我应该用它们的名称制作一个简单的矢量并仔细查看它们,但是我该怎么做呢?

I have a number of large DFs and I want to loop through them. Instead of binding them together to make a big list I thought I should make a simple vector with their names and look though them, but how can I do this?

例如我有:

DF1 <- data.frame(c("a", "b", "c"),c(TRUE, FALSE, TRUE))
DF2 <- data.frame(c("aa", "bb", "cc"),c(FALSE, FALSE, TRUE))
DF3 <- data.frame(c("aaa", "bbb", "ccc"),c(TRUE, FALSE, FALSE))

MyDFs <- c("DF1", "DF2", "DF3")

for (i in MyDFs) {
    print(nrow(i))
}

但是for循环不起作用,因为R无法将它们识别为DF,我该如何纠正呢?这也是做到这一点的最好方法吗?

but the for loop does not work as R does not recognise them as the DFs, how can I correct this? also is this the best way to do this?

推荐答案

 sapply(mget(MyDFs),nrow)
 #DF1 DF2 DF3 
 # 3   3   3 

如果您不想创建向量MyDFs

 sapply(mget(ls(pattern="DF")), nrow) #should also work

这篇关于R-从名称列表中循环遍历多个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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