简体   繁体   English

如果每个数据帧具有多于 n 行,则 R 循环用于在数据帧列表中添加数据帧

[英]R loop for adding dataframes in a dataframe list if each dataframe has more than n rows

I usually code in python and I'm struggling with this problem.我通常用 python 编码,我正在努力解决这个问题。

I have a list of dataframe names called dataframe_name and I would like to use this list to call some dataframes objects.我有一个名为 dataframe_name 的数据帧名称列表,我想使用这个列表来调用一些数据帧对象。 After that I want to use an if loop to pick up the dataframes with number of rows equal to 34 and list them in order to create a list of dataframe objects.之后,我想使用 if 循环来获取行数等于 34 的数据帧并列出它们以创建数据帧对象列表。

dataframe_name<-c("x1","x2","x3","x4","x5","x6","x7")
dataframe_list<-list()

for(i in dataframe_name){
  if(nrow(get(i))==34){
    append(get(i),dataframe_list)
  }
}

When I run the script I get this error:当我运行脚本时,我收到此错误:

Error in if (nrow(get(i)) == 34) { : argument is of length zero

What I have understood is that nrow(get(i)) appears to be null but I don't know why.我所理解的是 nrow(get(i)) 似乎为空,但我不知道为什么。

Thanks for any help.谢谢你的帮助。

You can try the following R code:您可以尝试以下 R 代码:

dataframe_name<-c("x1","x2","x3")
x1 <- data.frame(a=c(1, 2))
x2 <- data.frame(a=c(1, 2))
x3 <- data.frame(a=rep(1, 34))
dataframe_list<-list()

for(i in seq_along(dataframe_name)){ #note the different syntax for for loops in R
  if(nrow(get(dataframe_name[i]))==34){
    dataframe_list[[dataframe_name[i]]] <- get(dataframe_name[i]) #new element of the list by the original name of the dataframe
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为数据框列表 R 中的每个 dataframe 中的每个列名添加前缀 - Adding a prefix to each column name in each dataframe in a list of dataframes R 是否可以在 R 中创建一个循环,更改每个 dataframe 中数据框列表和排序行中某些变量的名称? - Is it possible to create a loop in R that changes the name of some variables in a list of dataframes and order rows in each dataframe? 通过每个数据帧中的行数对数据帧列表进行重新排序 - Reorder a list of dataframes by the number of rows in each dataframe 将数据帧拆分为任意数量的较小数据帧,行数不超过N - Split a dataframe into any number of smaller dataframes with no more than N number of rows 将数据帧附加到 r 中数据帧列表中每个数据帧的末尾 - Append dataframe to end of each dataframe in a list of dataframes in r R:将列表中每个数据帧的行重新组织为新数据帧的新列表 - R: Reorganizing rows of each dataframe within a list into a new list of new dataframes 列表中列表中的数据帧 - 如何访问 R 中的每个数据帧 - Dataframes within list within list - how to access each dataframe in R 将一个 dataframe 的变量添加到包含更多行的数据帧列表中 - Add variables of one dataframe to a list of dataframes which contain more rows 将索引列添加到列表 R 中的每个 dataframe - Adding index column to each dataframe in a list R 基于另一个 dataframe 中的行创建多个新数据帧,并在 r 中使用 for 循环 - Create multiple new dataframes based on rows in another dataframe with a for loop in r
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM