简体   繁体   English

group_by 并总结没有错误的丢弃值

[英]group_by and summarise dropping values with no error

I am using group_by and summarise to compute the sum of a column by a location variable in another column.我正在使用 group_by 和 summary 来计算另一列中位置变量的列的总和。 The dataframe is formatted as follows:数据框的格式如下:

Store  Sales
AB12   12
BD43   32
DC65   65
AB12   1
DC65   3
DC65   4
store_total <- store %>%
group_by(store) %>%
summarise(total_sales = sum(total_sales))

When I run this code, I get the results for AB12 and the rest are NA values.当我运行这段代码时,我得到了 AB12 的结果,其余的是 NA 值。 Is there something wrong with the code or should I be using a different function?代码有问题还是我应该使用不同的功能?

I use Microsoft R Open v 4.0我使用 Microsoft R Open v 4.0

Take a look at my answer:看看我的回答:

Loading library:加载库:

library(dplyr)

Reproducing your dataframe:重现您的数据框:

store = c("AB12",   12,
"BD43",   32,
"DC65",   65,
"AB12",   1,
"DC65",   3,
"DC65",   4)

store<-as.data.frame(matrix(store, ncol=2, byrow=T))
names(store)<-c("Store",  "Sales")
store$Store<-as.factor(store$Store)
store$Sales<-as.numeric(store$Sales)
store


> store
  Store Sales
1  AB12    12
2  BD43    32
3  DC65    65
4  AB12     1
5  DC65     3
6  DC65     4

Fixing group_by parameter ( Store instead of store ) and summarise ( Sales instead of total_sales )修复 group_by 参数( Store而不是store )和汇总( Sales而不是total_sales

store_total <- store %>%
    group_by(Store) %>%
    summarise(total_sales = sum(Sales))

To be honest I don't like tibbles that's why I go back to a normal dataframe.老实说,我不喜欢 tibbles 这就是为什么我回到正常的数据帧。

This is the final result:这是最终结果:

store_total <- as.data.frame(store_total)
store_total

> store_total
  Store total_sales
1  AB12          13
2  BD43          32
3  DC65          72

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM