Struggling to Create a Pivot Table in R

耗尽温柔 提交于 2019-12-07 14:54:33

问题


I am very, very new to any type of coding language. I am used to Pivot tables in Excel, and trying to replicate a pivot I have done in Excel in R. I have spent a long time searching the internet/ YouTube, but I just can't get it to work.

I am looking to produce a table in which I the left hand side column shows a number of locations, and across the top of the table it shows different pages that have been viewed. I want to show in the table the number of views per location which each of these pages.

The data frame 'specificreports' shows all views over the past year for different pages on an online platform. I want to filter for the month of October, and then pivot the different Employee Teams against the number of views for different pages.

specificreports <- readxl::read_excel("Multi-Tab File - Dashboard 
Usage.xlsx", sheet = "Specific Reports")

specificreportsLocal <- tbl_df(specificreports)
specificreportsLocal %>% filter(Month == "October") %>%
                     group_by("Employee Team") %>%

This bit works, in that it groups the different team names and filters entries for the month of October. After this I have tried using the summarise function to summarise the number of hits but can't get it to work at all. I keep getting errors regarding data type. I keep getting confused because solutions I look up keep using different packages.

I would appreciate any help, using the simplest way of doing this as I am a total newbie!

Thanks in advance, Holly


回答1:


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))




回答2:


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!



来源:https://stackoverflow.com/questions/53540862/struggling-to-create-a-pivot-table-in-r

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