Struggling to Create a Pivot Table in R

折月煮酒 提交于 2019-12-06 00:51:02

I'm not sure I correctly understand your need without a data sample, but this may work for you:

library(rpivotTable) specificreportsLocal %>% filter(Month == "October") rpivotTable(specificreportsLocal, rows="Employee Team", cols="page", vals="views", aggregatorName = "Sum")

Otherwise, if you do not need it interactive (as the Pivot Tables in Excel), this may work as well:

specificreportsLocal %>% filter(Month == "October") %>% group_by_at(c("Employee Team", "page")) %>% summarise(nr_views = sum(views, na.rm=TRUE))

let's see if I can help a bit. It's hard to know what your data looks like from the info you gave us. So I'm going to guess and make some fake data for us to play with. It's worth noting that having field names with spaces in them is going to make your life really hard. You should start by renaming your fields to something more manageable. Since I'm just making data up, I'll give my fields names without spaces:

library(tidyverse)
## this makes some fake data
## a data frame with 3 fields: month, team, value
n <- 100
specificreportsLocal <-
  data.frame(
    month = sample(1:12, size = n, replace = TRUE),
    team = letters[1:5],
    value = sample(1:100, size = n, replace = TRUE)
  )

That's just a data frame called specificreportsLocal with three fields: month, team, value

Let's do some things with it:

# This will give us total values by team when month = 10
specificreportsLocal %>% 
  filter(month == 10) %>%
  group_by(team) %>%
  summarize(total_value = sum(value))
#> # A tibble: 4 x 2
#>   team  total_value
#>   <fct>       <int>
#> 1 a             119
#> 2 b             172
#> 3 c              67
#> 4 d             229

I think that's sort of like what you already did, except I added the summarize to show how it works.

Now let's use all months and reshape it from 'long' to 'wide'

# if I want to see all months I leave out the filter and 
# add a group_by month
specificreportsLocal %>% 
  group_by(team, month) %>%
  summarize(total_value = sum(value)) %>%
  head(5) # this just shows the first 5 values
#> # A tibble: 5 x 3
#> # Groups:   team [1]
#>   team  month total_value
#>   <fct> <int>       <int>
#> 1 a         1          17
#> 2 a         2          46
#> 3 a         3          91
#> 4 a         4          69
#> 5 a         5          83

# to make this 'long' data 'wide', we can use the `spread` function 
specificreportsLocal %>% 
  group_by(team, month) %>%
  summarize(total_value = sum(value)) %>%
  spread(team, total_value)
#> # A tibble: 12 x 6
#>    month     a     b     c     d     e
#>    <int> <int> <int> <int> <int> <int>
#>  1     1    17   122   136    NA   167
#>  2     2    46   104   158    94   197
#>  3     3    91    NA    NA    NA    11
#>  4     4    69   120   159    76    98
#>  5     5    83   186   158    19   208
#>  6     6   103    NA   118   105    84
#>  7     7    NA    NA    73   127   107
#>  8     8    NA   130    NA   166    99
#>  9     9   125    72   118   135    71
#> 10    10   119   172    67   229    NA
#> 11    11   107    81    NA   131    49
#> 12    12   174    87    39    NA    41
Created on 2018-12-01 by the reprex package (v0.2.1)

Now I'm not really sure if that's what you want. So feel free to make a comment on this answer if you need any of this clarified.

Welcome to Stack Overflow!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!