R:如何对列表中数据框的多列求和? [英] R: How to sum multiple columns of data frames in a list?

查看:3497
本文介绍了R:如何对列表中数据框的多列求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对列表中的数据帧的多列求和,并且只显示总和而不显示(计算)输入列.这里是一个例子:

ls <- list(data.frame(a=1, b=5, c=3, d=2), data.frame(a=NA, b=2, c=7, d=9))

ls
[[1]]
  a b c d
1 1 5 3 2

[[2]]
   a b c d
1 NA 2 7 9

我的预期结果是:

ls2
[[1]]
  c new
1 3   8

[[2]]
  c new
1 7  11

任何想法如何做到这一点?到目前为止,我一直在尝试增强 answer 对于列表,没有成功并且没有忽略输入列(a,b,d).到目前为止,我尝试得很糟糕:

lapply(ls, function(x) x$e <- rowSums(x[,c("a", "b", "d")], na.rm=T)) 
and 
ls$e <- lapply(ls, function(x) rowSums(x[,c("a", "b", "d")], na.rm=T)) 

提前谢谢

修改: 感谢Aech和Abdou的回答,此示例可以很好地解决这个问题.但是,我有200多个列,您是否知道一种无需编写将保留的列的方法?就像删除用于计算的列一样,而不是命名所有列.

感谢您改进的代码,它可以很好地与示例数据配合使用.但是,如果没有我的真实数据集,则会出现以下错误:

Error in rowSums(x[, columns_to_sum], na.rm = T) : 
 'x' must be an array of at least two dimensions"

我的列表大约有96个矩阵,其中有200列和一行.但是我不知道如何为我的错误准备一个可重现的示例.有任何想法吗?

解决方案

您不应将列表命名为 ls ,因为ls是一个函数.

lapply(myList, function(x) data.frame(c=x$c, new = rowSums(x[,c("a", "b", "d")], na.rm=T))) 

这里是一个解决方案,您仅指定删除的列(编辑后):

dropped <- c("a", "b", "d")
lapply(myList, function(x) {
  x$new <- rowSums(x[,dropped], na.rm=T)
  x[!names(x) %in% dropped]
  }) 

i want to sum multiple columns of data frames in a list and only show the sum without showing the (calculation) input columns. Here an example:

ls <- list(data.frame(a=1, b=5, c=3, d=2), data.frame(a=NA, b=2, c=7, d=9))

ls
[[1]]
  a b c d
1 1 5 3 2

[[2]]
   a b c d
1 NA 2 7 9

my expected result is:

ls2
[[1]]
  c new
1 3   8

[[2]]
  c new
1 7  11

Any ideas how to do this? So far I tried to enhance this answer for lists, without success and without omiting the input columns (a,b,d). I tried so far lapply:

lapply(ls, function(x) x$e <- rowSums(x[,c("a", "b", "d")], na.rm=T)) 
and 
ls$e <- lapply(ls, function(x) rowSums(x[,c("a", "b", "d")], na.rm=T)) 

Thank you in advance

Edit: Thanks Aech and Abdou for your answers, which work fine with this example. However, I have >200 columns, do you know a way without writing the columns that will remain? Like deleting the columns that I use for the calculation, instead of naming all columns.

EDIT 2: Thanks for your improved code, it works well with the example data. However, with my true data set not... I get the following error:

Error in rowSums(x[, columns_to_sum], na.rm = T) : 
 'x' must be an array of at least two dimensions"

My list has about 96 matrices with 200 columns and one row. But I don´t know how to prepare a reproducible example of my error. Any ideas?

解决方案

You should not name your list ls, because ls is a function.

lapply(myList, function(x) data.frame(c=x$c, new = rowSums(x[,c("a", "b", "d")], na.rm=T))) 

Here is a solution where you specify the dropped columns only (after edit):

dropped <- c("a", "b", "d")
lapply(myList, function(x) {
  x$new <- rowSums(x[,dropped], na.rm=T)
  x[!names(x) %in% dropped]
  }) 

这篇关于R:如何对列表中数据框的多列求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆