r - 计数字符并汇总每组的值
问题描述
我似乎可以为我的问题找到合适的代码。我想根据不同的条件创建组并汇总(总和、计数或长度)其他列。
我试过 group_by 并用不同的条件进行总结,但还没有找到任何可行的方法。
我有一个类似的表:
data <- data.frame(Name= c('Anna', 'Anna', 'Anna', 'Anna', 'Anna',
'Bella', 'Bella', 'Bella', 'Camilla', 'Camilla'),
Date= c('1.1.2021', '1.1.2021', '2.1.2021', '3.1.2021', '3.1.2021',
'1.1.2021', '5.1.2021', '5.1.2021', '7.1.2021', '8.1.2021'),
Item= c('Apple','Pear', 'Zucini','Apple', 'Broccoli',
'Apple','Pear','Apple','Apple', 'Tomato'),
Category= c('Fruit', 'Fruit', 'Vegetable', 'Fruit', 'Vegetable',
'Fruit', 'Fruit', 'Fruit', 'Fruit', 'Vegetable'),
Weight_kg= c(0.2,0.3,0.5,0.4,1.1,
1,0.5,0.8,1.2,0.5)
)
这将是我想要的输出:
desired_table <- data.frame(Name=c('Anna', 'Bella', 'Camilla'),
Shopping_days=c(3,2,2),
days_fruit=c(2,2,1),
days_vegetables=c(2,0,1),
Total_kg=c(2.5,2.3,1.7),
Fruit_kg=c(0.9,2.3,1.2),
Vegetables=c(1.6,0,0.5))
我尝试了许多与此类似的代码变体,但显然不起作用:
data1 <- data %>%
group_by(Name) %>%
summarize(Shopping_days = length(unique(Date)),
days_fruit = length(unique(Date, Category='Fruit')),
days_vegetables = length(unique(Date, Category='Vegetables')),
Total_kg = sum(Weight_kg),
Fruit_kg = sum(Weight_kg, if Category=Fruit),
Vegetables_kg = sum(Weight_kg, if Category=Vegetables))
任何帮助将非常感激。
解决方案
使用group_by
和summarise
:
library(dplyr)
data %>%
group_by(Name) %>%
summarise(Shopping_days = n_distinct(Date),
days_fruit = n_distinct(Item[Category == 'Fruit']),
days_vegetables = n_distinct(Item[Category == 'Vegetable']),
Total_kg = sum(Weight_kg),
Fruit_kg = sum(Weight_kg[Category == 'Fruit']),
Vegetables_kg = sum(Weight_kg[Category == 'Vegetable']))
# Name Shopping_days days_fruit days_vegetables Total_kg Fruit_kg Vegetables_kg
# <chr> <int> <int> <int> <dbl> <dbl> <dbl>
#1 Anna 3 2 2 2.5 0.9 1.6
#2 Bella 2 2 0 2.3 2.3 0
#3 Camilla 2 1 1 1.7 1.2 0.5
推荐阅读
- python-pptx - 通过 PPTX 访问拼写检查
- docx4j - 与 docx4j 的富文本内容控件数据绑定
- javascript - 在滑动时反应选择多个元素?
- f# - F# Deedle 将 Frame 保存到 MySql 数据库
- javascript - 与 inferno 相比,Svelte 的“评估脚本”时间似乎更长,preact
- reactjs - 出现错误:“您在预期流的位置提供了一个无效对象”,而没有 react-native 调试器
- javascript - React 函数的行为不像预期的那样
- excel - 将工作簿中的单个工作表作为 Outlook 的附件发送
- laravel - laravel 通知插入错误的表列
- c++ - 错误:使用复制和交换习语的交换函数中“operator=”的重载不明确