gpt4 book ai didi

r - 将多列之一迭代合并到列表中的数据框

转载 作者:行者123 更新时间:2023-12-02 07:59:39 26 4
gpt4 key购买 nike

在将单个数据框的列与列表中的其他数据框迭代组合时,我遇到了一个(可能很小的)问题。一些数据来说明:

# load example data
library(vegan)
data(varechem)
data(varespec)

# generate predictor tables with overlapping rows and different amount of cols
varespec1 <- varespec[c(1:9), ]
varespec2 <- varespec[c(8:16), c(1:43)]
varespec3 <- varespec[c(14:24), c(1:41)]

# store predictor tables in list
subset_list <- list(varespec1 = varespec1,
varespec2 = varespec2,
varespec3 = varespec3)

# generate a table that holds ALL possible response variables as presence/absence
varechem_binary <- as.data.frame(apply(varechem, 2, cut,
breaks = c(-Inf, 1.0, Inf), labels = c("Absent", "Present")))
row.names(varechem_binary) <- row.names(varechem)

上面的代码说明了我是如何为分类任务准备数据的。现在的想法是 data.frames列表中包含预测变量 ( varespec1 , ...) 应用于预测响应表 ( varechem_binary ) 中的每一列,但一次只能预测一个。将响应表与每个预测变量表合并很容易:

# merge response table with each predictor table
merge_counter <- 0
merged_list <- list()
for(table in subset_list) {
merge_counter <- merge_counter + 1
current_name <- names(subset_list)[merge_counter]
tmp <- merge(table, varechem_binary, by = "row.names")
row.names(tmp) <- tmp$Row.names
tmp <- tmp[, -1]
merged_list[[current_name]] <- tmp
rm(tmp)
}

预期输出:

我现在(或者在代码的前面,如果这更有意义的话)正在寻找一种方法,将每个预测变量表与响应表 varechem 中的每个列和恰好一个列组合起来。在列表中。这基本上是:

# storing in data frames just for illustration, I would like to do this within the list
# subsets for the 3 predictor tables with the first response variable
aa <- merged_list[[1]][,-c(46:58)] # column 1:44 are the predictor variables, then the different response variables start
bb <- merged_list[[2]][,-c(45:57)] # column 1:43 are the predictor variables, then the different response variables start
cc <- merged_list[[3]][,-c(43:58)] # column 1:41 are the predictor variables, then the different response variables start

# subsets for the 3 predictor tables with the second response variable
dd <- merged_list[[1]][,-c(45, 47:58)]
ee <- merged_list[[2]][,-c(44, 46:57)]
ff <- merged_list[[3]][,-c(42, 44:58)]

# subsets for the 3 predictor tables with the third response variable
gg <- merged_list[[1]][,-c(45, 46, 48:58)]
...

# this is just to illustrate how the list could look like, I would like to keep all files in a list all the time
list_for_classification_runs <- list(aa, bb, cc, dd, ee, ff, gg, ...)

此结果列表将是随机森林分类调用的输入,其中响应变量将按来自 varespec 的所有其他预测变量进行分类。比如:

for (current_table in list_for_classification_runs) {
counter <- counter + 1
# response_variable should be the one variable added to the predictor variables in the data frames
RF_list[[counter]] <- ranger(response_variable ~ ., data = current_table)
}

最佳答案

根据 Gregor 的评论,我想出了一个类似的方法。我没有将完整的 varechem_binarysubset_list 的所有元素合并,而是添加了另一个 for 循环并迭代了 varechem_binary 中的所有列。使用 drop = FALSE 保留 row.names 和结构,因此合并有效:

merge_col_counter <- 0
column_counter <- 0
merged_column_list <- list()

for(table in subset_list) {
merge_col_counter <- merge_col_counter + 1
for (column in names(varechem_binary)) {
column_counter <- column_counter + 1
current_name <- paste(names(subset_list)[merge_col_counter], names(varechem_binary)[column_counter], sep = "_")
print(current_name)
tmp <- merge(table, varechem_binary[, column_counter, drop = FALSE], by = "row.names")
row.names(tmp) <- tmp$Row.names
tmp <- tmp[, -1]
merged_column_list[[current_name]] <- tmp
rm(tmp)
}
column_counter <- 0
}

可能有一些方法可以做到这一点更清洁或更有效,但它有效,所以我可以继续

关于r - 将多列之一迭代合并到列表中的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59198880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com