r - 在R中对名称旁边的行求和
问题描述
我正在从事一个银行项目,我试图找到每年花费的金额,而数据集将这些列为每月交易。
Month Name Money Spent
2 John Smith 10
3 John Smith 25
4 John Smith 20
2 Joe Nais 10
3 Joe Nais 25
4 Joe Nais 20
现在,这是我的代码:
OTData <- OTData %>%
mutate(
OTData,
Full Year = [CODE NEEDED TO SUM UP]
)
谢谢!
解决方案
正如@Pawel 所说,这里没有问题。我假设你想要:
df <- data.frame(Month = c(2,3,4,2,3,4),
Name = c("John Smith", "John Smith", "John Smith",
"Joe Nais", "Joe Nais", "Joe Nais"),
Money_Spent = c(10,25,20,10,25,20))
df %>%
group_by(Name) %>%
summarize(Full_year = sum(Money_Spent))
Name Full_year
<fct> <dbl>
1 Joe Nais 55
2 John Smith 55
注意:如果在变量名中包含空格,将会遇到麻烦。您确实应该将它们替换为.
, _
, 或camelCase
如上例所示。
推荐阅读
- android - 如何生成具有多个类型的相同对象的请求模型
- r - R数据帧切片
- android - 我如何解析 Json 对象或数组?我的代码哪里出错了?
- c# - 根据当前用户登录到全名从 SQL 表中获取指定值
- java - 在其他 Activity 中传递数据的 Android 问题
- wordpress - Wordpress > 使用相同的 slug 将 301 从域 A 重定向到域 B
- c# - Windows 上的红隼和 macOS/Linux 上的红隼之间的日期反序列化差异
- swift - swift framework:代码改变了,构建的框架没有更新?
- java - 如何从 aws sdk java 中检索实例 user_data
- odoo - 如何通过 Odoo 13 中的按钮在 SAVE 或 VIEW 模式下打开包含现有记录的表单视图?