【发布时间】:2016-11-29 08:24:21
【问题描述】:
我有一个包含多个变量的 data.frame,我需要根据它们名称中的模式对它们进行求和。更具体地说,我的股票总和为 1,不包括我需要找出的可能的残差。我为此使用dplyr。
一个示例数据帧:
df <- data.frame(year = c(2000, 2001, 2002),
aShare = c(.1,.2,.3),
bShare = c(.3,.4,.5))
我尝试过像这样使用ends_with 函数:
tmp <- df %>% mutate(otherShare = 1 - sum(ends_with("Share")))
但它不会产生所需的结果:
TMP <- df %>% mutate(otherShare = 1 - (aShare + bShare))
【问题讨论】: