Last active
August 24, 2021 19:43
-
-
Save madilk/50dd55394b93bae39d855d1577db5f58 to your computer and use it in GitHub Desktop.
dplyr - mutate , subset and group_by
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Padr package method | |
#https://statisticsglobe.com/insert-rows-for-missing-dates-in-r | |
#install.packages("padr") | |
library(padr) | |
offerdata_padr <- pad(offerdata) | |
View(offerdata_padr) | |
#https://www.statology.org/dplyr-replace-na-with-zero/ | |
#Add rows for missing dates and replace NAs with zero | |
offerdata2<- offerdata%>% | |
complete(Date = seq.Date(min(Date), max(Date), by="day"))%>% | |
replace(is.na(.), 0) | |
View(offerdata2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Create column 'inoffer' where > Mark rows as in offer or not | |
#(only John Wick or John Wick 2) | |
#Also, Date must be > 2021-01-01 | |
offerdata<- data%>% | |
mutate(inOffer= | |
ifelse(str_detect(Item,"^John Wick$|^John Wick 2$"), | |
"In offer", | |
"Not in offer")) %>% | |
subset(inOffer=="In offer"& | |
data$Date>as.Date("2021-01-01")) %>% | |
group_by(Date) %>% | |
summarise(Quantity_sold=sum(Quantity_sold)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment