从data.frame列表中排除向量

时间:2013-01-31 23:17:11

标签: r

我有50个data.frame对象的列表,每个data.frame对象包含20行。我需要在每次迭代时从每个data.frame对象中排除一行或一个向量。

单次迭代可能如下所示:

to_exclude <- 0  # 0 will be replaced by the induction variable
training_temp <- lapply(training_data, function(x) {
                                        # Exclude the vector numbered to_exclude
                                        }

此致

1 个答案:

答案 0 :(得分:0)

df <- data.frame(x=1:10,y=1:10)

thelist <- list(df,df,df,df)

lapply(thelist, function(x) x[-c(1) ,] )

这将始终删除第一行。这是你想要的,还是你想根据值删除行?

这将始终排除第一列:

lapply(thelist, function(x) x[, -c(1) ] )

# because there are only two columns in this example you would probably 
# want to add drop = FALSe e.g.
# lapply(thelist, function(x) x[, -c(1), drop=FALSE ] )

所以从你的循环值:

remove_this_one <- 10
lapply(thelist, function(x) x[ -c(remove_this_one) ,] )
# remove row 10