对具有相似名称的列按行求和 [英] Sum columns row-wise with similar names

查看:25
本文介绍了对具有相似名称的列按行求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 dataframe,其中有很多类似这样的列:

I have a dataframe that has lots of columns that are something like this:

data <- data.frame (a.1 = 1:5, a.2b = 3:7, a.5 = 5:9, bt.16 = 4:8, bt.12342 = 7:11)

我想要一个包含对具有相同前缀的变量求和的列的结果.在这个例子中,我想返回一个数据框:a = (9:13), bt = (11:15)

I'd like a result with columns that sum the variables that have the same prefix. In this example, I want to return a dataframe: a = (9:13), bt = (11:15)

我的真实数据集相当复杂(我想结合具有不同 utm 参数的网页的页面查看次数),但这种情况的解决方案应该让我走上正轨.

My real data set is quite a bit more complicated (I want to combine page view counts for web pages with different utm parameters) but a solution for this case should put me on the right track.

推荐答案

这里有一个基于 R 的解决方案:

Here a solution with base R:

> prefixes = unique(sub("\..*", "", colnames(data)))
> sapply(prefixes, function(x)rowSums(data[,startsWith(colnames(data), x)]))
      a bt
[1,]  9 11
[2,] 12 13
[3,] 15 15
[4,] 18 17
[5,] 21 19

这篇关于对具有相似名称的列按行求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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