简体   繁体   English

在R中的for循环中循环多个数据帧

[英]Loop over multiple dataframes in a for loop in R

I have four dataframes A,B,C,D. 我有四个数据框A,B,C,D。 I want to iterate over these four dataframes so that each of them is passed to the custom function testdf() as the fourth parameter which can take only dataframe data type. 我想遍历这四个数据框,以便将它们中的每个传递给自定义函数testdf()作为第四个参数,该参数只能采用数据框数据类型。

for (a in 1: (A,B,C,D)){
  a<-testdf(x,y,z,A)
}

I also tried using list but that didn't seem to work as even when I passed as.data.frame(mylist(A)) in the function it threw an error that list can't be passed. 我也尝试使用列表,但是即使在函数中传递as.data.frame(mylist(A))时,它似乎也不起作用,它引发了一个错误,即无法传递列表。

The way you have your code written it seems like there are variable mix -ups. 您编写代码的方式似乎存在变量混合。 My example below should address that. 我下面的例子应该解决这个问题。

Using a list like you tried previously might be a good option. 使用您之前尝试过的列表可能是一个不错的选择。


A <- as.data.frame(0,matrix(0, nrow = 4, ncols = 6)
B <- as.data.frame(0,matrix(0, nrow = 5, ncols = 6)
C <- as.data.frame(0,matrix(0, nrow = 4, ncols = 4)
D <- as.data.frame(0,matrix(0, nrow = 3, ncols = 5)

list.dfs <- list(A,B,C,D)

for (i in 1:length(list.dfs)){

#Since I don't know your function I just catenated the letters with
#whatever is in your data frames
  result <- cat("a","b","c",i)

}

Let me know if that helps any or if you need clarification! 让我知道这是否有帮助或您是否需要澄清!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM