Here's a data frame:
sample_data <- data.frame(
Device = c("Desktop", "Desktop", "Desktop", "Tablet", "Tablet", "Tablet", "Mobile", "Mobile", "Mobile"),
Date_Range = seq(as.Date("2017-01-01"), as.Date("2017-01-30"), by = 1),
Sessions = ceiling(rnorm(90, mean = 3000, sd = 300)),
Conversion1 = ceiling(rnorm(90, mean = 800, sd = 100)),
Conversion2 = ceiling(rnorm(90, mean = 400, sd = 50))
)
And it looks like this:
head(sample_data)
Device Date_Range Sessions Conversion1 Conversion2
1 Desktop 2017-01-01 2906 920 361
2 Desktop 2017-01-02 2604 873 380
3 Desktop 2017-01-03 2449 781 398
4 Tablet 2017-01-04 3205 851 534
5 Tablet 2017-01-05 3099 622 341
6 Tablet 2017-01-06 2708 842 386
If there was only one metric (there's actually 3: Sessions + 2 conversion columns) and I wanted columns as dates for a trended table I could go:
sample_data_spread <- tbl_df(sample_data) %>%
group_by(Date_Range) %>%
summarize(Sessions = sum(Sessions)) %>%
spread(key = Date_Range, value = Sessions)
This looks like:
> sample_data_spread[,1:5]
# A tibble: 1 × 5
`2017-01-01` `2017-01-02` `2017-01-03` `2017-01-04` `2017-01-05`
<dbl> <dbl> <dbl> <dbl> <dbl>
1 9238 8937 9129 9384 8222
But what I actually want is a table with the following format:
- columns: the dates like I have now
- rows: sessions, Conversion1, Conversion2
- I'm ignoring Device column for now
What is a good way to do this?