总和列基于其他列中的可变名称,该名称包含x,按照相似的字母
我有一个类似这样的表:
var | rc |
---|---|
dance50 | 2 |
距离20 | 4 |
precmax | 5 |
precmin | 1 |
tote_prec | 8 |
旅行时间 | 5 |
旅行时间 | 2 |
我想总结所有类似类型的变量,从而产生这样的东西:
var | sum sum |
---|---|
6 | prec |
dist | 14 prec 14 |
trav | 7 |
使用使用4个字母足以分开不同类型。我已经尝试并尝试过,但没有弄清楚。有人可以协助吗?我通常会尝试与dplyr一起工作,因此这是首选。数据集很小(N< 100),因此不需要速度。
I have a table that is somewhat like this:
var | RC |
---|---|
distance50 | 2 |
distance20 | 4 |
precMax | 5 |
precMin | 1 |
total_prec | 8 |
travelTime | 5 |
travelTime | 2 |
I want to sum all similar type variables, resulting in something like this:
var | sum |
---|---|
dist | 6 |
prec | 14 |
trav | 7 |
Using 4 letters is enough to separate the different types. I have tried and tried but not figured it out. Could anyone please assist? I generally try to work with dplyr, so that would be preferred. The datasets are small (n<100) so speed is not required.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本R解决方案:
数据:
Base R solution:
Data:
通过在下划线上拆分(
az az 0 -9 。_
),大写字母group_by
语句中,如果可以在第一个列中找到第二列,则填写第一列。rc
。输出
separate
var
into two columns by splitting on underscores (_
), capital lettersA-Z
or numbers0-9
.group_by
statement, if the second column can be found in the first then fill the first column.RC
by group.Output