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

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

问题描述

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

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

我的预期结果是:

ls2
[[1]]
  c new
1 3   8

[[2]]
  c new
1 7  11

任何想法如何做到这一点?到目前为止,我试图增强这个 answer对于列表,没有成功并且没有省略输入列(a,b,d).到目前为止,我尝试了 lapply:

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)) 

提前谢谢你

编辑:感谢 Aech 和 Abdou 的回答,在这个例子中效果很好.但是,我有超过 200 列,你知道不写将保留的列的方法吗?就像删除我用于计算的列,而不是命名所有列.

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.

编辑 2:感谢您改进的代码,它适用于示例数据.但是,我的真实数据集不是......我收到以下错误:

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"

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

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?

推荐答案

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

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天全站免登陆