I am using group_by and summarise to compute the sum of a column by a location variable in another column. 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. Is there something wrong with the code or should I be using a different function?
I use 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
)
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.