I have a question regarding dataframe manipulation. I have a dataframe which looks like this:
year | month | deviceCategoy | sessions
2017 | 4 | desktop | 140000
2017 | 4 | mobile | 200000
2017 | 4 | tablet | 80000
... ... ... ...
All in all the dataframe contains data for the whole year 2017 and 2018 until now. Now I want to have a new dataframe, where I only have the device categories desktop and mobile. The sessions for the tablets should be added to desktop.
The result should look like this:
year | month | deviceCategoy | sessions
2017 | 4 | desktop | 220000
2017 | 4 | mobile | 200000
Does anybody knows how to do this?
We could change the "tablet" strings to "desktop" and then do an aggregate
i1 <- df1$deviceCategoy == "tablet"
df1$deviceCategoy[i1] <- "desktop"
aggregate(sessions ~ ., df1, sum)
# year month deviceCategoy sessions
#1 2017 4 desktop 220000
#2 2017 4 mobile 200000
Or using tidyverse
library(dplyr)
df1 %>%
mutate(deviceCategoy = replace(deviceCategoy, deviceCategoy == "tablet", "desktop")) %>%
group_by_at(names(.)[1:3]) %>%
summarise(sessions = sum(sessions))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With