0

我有一个包含 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
                                        }

问候

4

1 回答 1

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
于 2013-01-31T23:37:45.050 回答