对于任何类型的编码语言,我都是非常新的。我习惯于在Excel中使用数据透视表,并尝试复制在R中的Excel中完成的数据透视。我花了很长时间搜索Internet / YouTube,但我无法使其正常工作。
我正在寻找一个表格,其中左侧列显示了多个位置,表格顶部显示了已查看的不同页面。我想在表格中显示每个页面中每个位置的观看次数。
“特定报告”数据框显示了过去一年在线平台上不同页面的所有视图。我想过滤十月份,然后针对不同页面的视图数量来调整不同的员工团队。
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") %>%
该位起作用,因为它将不同的团队名称分组并筛选十月份的条目。在此之后,我尝试使用summary函数来汇总命中数,但根本无法正常工作。我不断收到有关数据类型的错误。我一直感到困惑,因为我查找的解决方案一直使用不同的软件包。
我很乐意提供帮助,因为我是新手,所以请使用最简单的方法!
预先感谢, 冬青树
答案 0 :(得分:0)
我不确定没有数据样本是否能正确理解您的需求,但这可能对您有用:
library(rpivotTable)
specificreportsLocal %>% filter(Month == "October")
rpivotTable(specificreportsLocal, rows="Employee Team", cols="page", vals="views", aggregatorName = "Sum")
否则,如果您不需要它是交互式的(如Excel中的数据透视表),这可能也可以工作:
specificreportsLocal %>% filter(Month == "October") %>%
group_by_at(c("Employee Team", "page")) %>%
summarise(nr_views = sum(views, na.rm=TRUE))
答案 1 :(得分:0)
让我看看能不能帮上忙。从您提供给我们的信息中很难知道您的数据是什么样的。因此,我将猜测并制作一些虚假数据供我们使用。值得注意的是,字段名称中带有空格将使您的生活变得非常艰难。您应该首先将字段重命名为更易于管理的名称。由于我只是在整理数据,因此我要给字段名称加上空格:
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)
)
那只是一个名为specificreportsLocal
的数据帧,其中包含三个字段:month
,team
,value
让我们用它做一些事情:
# 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
我认为这与您已经做过的一样,只是我添加了摘要以显示其工作原理。
现在让我们使用所有月份并将其形状从“长”更改为“宽”
# 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)
现在,我不确定是否正是您想要的。因此,如果您需要任何澄清的内容,请随时对此答案发表评论。
欢迎堆栈溢出!