简体   繁体   English

R- for 循环选择数据框中的两列,只有第二列发生变化

[英]R- for loop to select two columns in a data frame, with only the second column changing

I'm having issues trying to write a for loop in R. I have a dataframe of 16 columns and 94 rows and i want to loop through, selecting column 1, plus column 2 in one data frame, then col 1 + col 3 etc, so i end up with 16 dataframes containing 2 columns, all written to individual .csv files我在尝试在 R 中编写 for 循环时遇到问题。我有一个 16 列和 94 行的数据框,我想循环遍历,在一个数据框中选择第 1 列和第 2 列,然后是第 1 列 + 第 3 列等,所以我最终得到包含 2 列的 16 个数据帧,全部写入单独的 .csv 文件

TwoB<- read.csv("data.csv", header=F) 

list<- lapply(1:nX, function(x) NULL)


nX <- ncol(TwoB)

for(i in 1:ncol(TwoB)){
list[[i]]<-subset(TwoB,
                 select=c(1, i+1))
 }

Which produces an error:这会产生一个错误:

 Error in `[.data.frame`(x, r, vars, drop = drop): 
   undefined columns selected

I'm not really sure how to code this and clearly haven't quite grasped loops yet so any help would be appreciated!我不太确定如何对此进行编码,并且显然还没有完全掌握循环,因此将不胜感激!

The error is easily explained as you loop over 16 columns and in the end trying to select 16+1 which column index does not exists.当您循环遍历 16 列并最终尝试选择 16+1 哪个列索引不存在时,该错误很容易解释。 You probably could loop over nX-1 instead, but I think what you try to achieve can be done more elegant.您可能可以改为循环使用 nX-1,但我认为您尝试实现的目标可以做得更优雅。

TwoB<- read.csv("data.csv", header=F)

library("data.table")
setDT(TwoB)

nX <- ncol(TwoB)

# example to directly write your files
lapply(2:nX, function(col_index) {
    fwrite(TwoB[, c(1, ..col_index)], file = paste0("col1_col", col_index, ".csv"))
})

# example to store the new data.tables in a list
list_of_two_column_tables <- lapply(2:nX, function(col_index) {
    TwoB[, c(1, ..col_index)]
})

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

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