My data frame:
date | weekday | price
2018 | 1 | 25
2018 | 1 | 35
2019 | 2 | 40
I try to run this code under dplyr:
pi %>%
group_by(date) %>%
group_by(date) %>%
summarise(price = sum(price, na.rm = T)) %>%
select(price, date, weekday) %>%
print()
It doesn't work.
Any solution? Thanks in advance
Follow the order: select-->group_by-->summarise
df%>%select(price, date, weekday)%>%
group_by(date, weekday)%>%summarise(sum(price,na.rm=T))
People are correctly suggesting to group_by
date and weekday, but if you have a lot of columns, that could be a pain to write out. Here's another idiom I frequently use for data.frames with lots of columns:
pi %>%
group_by(date) %>%
mutate(price = sum(price, na.rm = T)) %>%
filter(row_number() == 1)
This will keep all the first instances of each column variables without having to explicitly write them all out.
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.