本文介绍了在R的数据表中获取rowSums的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找一种简便的方法来将我的数据表中的总计约170个不同的列作为新列。我可以使用一些通配符表示吗?

Was looking for a easy way to get the sum total of around 170 different columns in my data table as a new column. Is there some wild card notation I can use?

以下是我的数据集中的一小部分摘录(前几列):

This following is a small extract (first few columns) from my dataset:

> head(t_checkin)
checkin_info_0.0 checkin_info_0.1 checkin_info_0.2 checkin_info_0.3 checkin_info_0.4 checkin_info_0.5
              NA               NA               NA               NA               NA                1
               3               NA               NA               NA               NA                1
              NA               NA                1               NA               NA               NA
              NA               NA               NA               NA               NA               NA
              NA               NA               NA               NA               NA               NA
              NA               NA               NA               NA               NA               NA

任何帮助将不胜感激。
谢谢

Any help would be greatly appreciated. Thanks

推荐答案

编辑

更正,执行@MatthewDowle所说的话:

Correction, do what @MatthewDowle says:

dat <- data.frame(x=1:11,y=100:110,z=sample(letters,11))
DT <- as.data.table(dat)
names <- c("x","y")
DT[,lapply(.SD,sum),.SDcols=names]


mysum <- function(x){sum(x, na.rm=TRUE)}
DT[,lapply(.SD,mysum),.SDcols=names]

这篇关于在R的数据表中获取rowSums的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:21