用R语言构建一个list每个list是一个三行两列的数据框。然后对list里每一个数据框的第2列求和放到list中。
您可以使用以下代码来构建一个包含多个数据框的列表,并对每个数据框的第2列求和并将结果放到列表中的方法:
# 构建数据框
df1 <- data.frame(Col1 = c(1, 2, 3), Col2 = c(4, 5, 6))
df2 <- data.frame(Col1 = c(7, 8, 9), Col2 = c(10, 11, 12))
df3 <- data.frame(Col1 = c(13, 14, 15), Col2 = c(16, 17, 18))
# 构建列表
my_list <- list(df1, df2, df3)
# 对每个数据框的第2列求和并放到列表中
sum_list <- lapply(my_list, function(x) sum(x$Col2))
# 输出结果
print(sum_list)
这样,sum_list 列表中的每个元素分别表示 my_list 列表中对应数据框的第2列的和。
原文地址: https://www.cveoy.top/t/topic/imuj 著作权归作者所有。请勿转载和采集!