[英]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.