简体   繁体   English

R遍历组和列并将数据输出到帧中

[英]R Loop through group and column and output data into frame

I'm trying to create a loop by group to save me having to test all variables in mctest for multicolinearity for a large dataset. 我试图按组创建一个循环,以免我不得不在mctest中测试所有变量的大型数据集的多重共线性。 As an example: 举个例子:

library(mctest)
library(AER)
library(dplyr)

iris <- datasets::iris
iris$stem <- sample(150, size = nrow(iris))
iris$lifespan <- sample(150, size=nrow(iris))
vif_results <- data.frame()

iris_setosa <- iris[which(iris$Species == "setosa"),]
i6 <- imcdiag(iris_setosa[,c(1:4)],iris_setosa[,6]) ## works
i7 <- imcdiag(iris_setosa[,c(1:4)],iris_setosa[,7]) ## works

##copy to df
i6_res <- data.frame(i6$idiags)
i6_res$group <- "setosa"
i6_res$Out <- "stem"

i7_res <- data.frame(i7$idiags)
i7_res$group <- "setosa"
i7_res$Out <- "life"

vif_results <- rbind(i6_res,i7_res)

##Rather than doing the above manually by Species and columns, write a loop to iterate through groups and columns 
for(col in 1:ncol(iris[,c(6:7)])){
  require(dplyr)
  iris1 <- iris %>%
  group_by(Species) %>% 
  do(imcdiag(iris[,c(1:4)],iris[,col]))
print(iris1)
}

f1 <- function(x){
  require(dplyr)
  iris1 <- iris %>%
    group_by(Species) %>% 
    do(imcdiag(iris[,c(1:4)],iris[,x]))
  return(f1)
}

for(col in 1:ncol(iris[,c(6:7)])){print(f1)}

The loops I've tried in various formats don't work. 我尝试过的各种格式的循环均无效。 Thanks! 谢谢!

Edit 编辑

I've been playing with this and have been able to build the data frame in loop (I know that's not preferred). 我一直在玩这个游戏,并且能够循环构建数据框架(我知道这不是首选)。 I still can't get the second loop to work to run the columns through the y-values so I've copied & edited to show what I'm trying to code with the 2nd y-value. 我仍然无法通过第二个循环来遍历y值的列,因此我已经复制并编辑以显示我要使用第二个y值编码的内容。

 for (i in seq_along(species)) { all <- subset(iris, iris$Species == species[i]) ## This is the part I wanted to create another loop for so that I don't have to list a lot of y-variables for bigger datasets y6 <- imcdiag(all[,1:4],all[,6]) ## This section works nicely to append all the vif values into a single table for easy analysis and export. vifs6 <- as.data.frame(y6$idiags) vifs6$Iteration <- i vifs6$Species <- species[[i]] vifs6$Output <- paste0(colnames(iris[6])) vif_results <- rbind(vif_results,vifs6) print(y6) y7 <- imcdiag(all[,1:4],all[,7]) vifs7 <- as.data.frame(y7$idiags) vifs7$Iteration <- i vifs7$Species <- species[[i]] vifs7$Output <- paste0(colnames(iris[7])) vif_results <- rbind(vif_results,vifs7) print(y7) } 

Any suggestions would be greatly appreciated! 任何建议将不胜感激!

With apply you can achieve your desired output, a data frame with results for all combinations of group and columns of interest. 通过apply您可以实现所需的输出,即包含感兴趣的组和列的所有组合的结果的数据框。

We'll use the iris data frame with two extra columns, as described in your question 如您的问题所述,我们将iris数据框与另外两列一起使用

First, we create a data frame with all combinations or group and column of interest with expand.grid . 首先,我们使用expand.grid创建具有所有组合或感兴趣的组和列的数据框。

combinations <- expand.grid(names(iris[, 6:7]), unique(as.character(iris[["Species"]])))

Results in this: 结果:

      Var1       Var2
1     stem     setosa
2 lifespan     setosa
3     stem versicolor
4 lifespan versicolor
5     stem  virginica
6 lifespan  virginica

We have column names in the first column and group names in the second, so each row represents a pair of names. 我们在第一列中有列名,在第二列中有组名,因此每一行代表一对名称。

With this, we can loop over each pair using apply . 这样,我们可以使用apply遍历每一对。

results_list <- apply(combinations, 1, function(each_pair) {
  # An iris subset
  this_iris <- iris[which(iris$Species == each_pair[2]),]

  # Results for that iris subset and one of the columns
  result <- imcdiag(this_iris[,c(1:4)], this_iris[each_pair[1]])

  # Extract idiags as a data frame
  result_df <- as.data.frame(result$idiags)

  # Add columns with names for the group, column and variables
  result_df[["column"]] <- each_pair[1]
  result_df[["group"]] <- each_pair[2]
  result_df[["variable"]] <- rownames(result_df)
  result_df
})

We end up with a list of data frames. 我们最后得到一个数据帧列表。 We can bind them together using do.call . 我们可以使用do.call将它们绑定在一起。

iris_idiags <- do.call(args = results_list, what = rbind)

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

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