所以这个数据框有很多单独的观察,我需要将它们加在一起。
使用 dget():
我尝试了很多不同的解决方案,例如:
df %>%
group_by(product, price) %>%
summarise(
quantity = sum(quantity),
total = sum(total)
)
和:
df %>%
gather(key = variable, value = value, c(Quantity,Price,Total)) %>%
group_by(Product, variable) %>%
summarize(sum = sum(value)) %>%
spread(variable, sum)
和:
df %>%
group_by(Product) %>%
summarise(Quantity = sum(Quantity),
AveragePrice = sum(Total)/sum(Quantity),
Total = sum(Total))
但我只是得到:
> df
quantity total
1 61 1685
预期的输出是这样的,但显然product列中的所有产品:
#> # Groups: product [2]
#> product price quantity total
#> <chr> <dbl> <dbl> <dbl>
#> 1 small cucumber 10 1 10
#> 2 tomatoes 1kg 16 2 32
我以前问过这个,但我还不够具体。
谢谢。