r - 按范围调整 pivot_wider 的整洁方法(由两列中的值给出)?

标签 r dplyr tidyr

我正在尝试转动它

df <- structure(list(id = c(38320858L, 38408709L, 38314694L, 38285286L, 
38332258L), type = c("recreation", "business", "friends", "business", 
"recreation"), start_week = c(6, 8, 6, 6, 7), end_week = c(11, 
10, 11, 10, 11)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

# A tibble: 5 x 4
        id type       start_week end_week
     <int> <chr>           <dbl>    <dbl>
1 38320858 recreation          6       11
2 38408709 business            8       10
3 38314694 friends             6       11
4 38285286 business            6       10
5 38332258 recreation          7       11

进入这个:

result <- structure(list(type = c("recreation", "business", "friends", 
"recreation", "business", "friends", "recreation", "business", 
"friends", "recreation", "business", "friends", "recreation", 
"business", "friends", "recreation", "friends"), week = c(6L, 
6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 
11L), n = c(1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 
2L, 1L, 2L, 1L)), row.names = c(NA, -17L), class = "data.frame")

#          type week n
# 1  recreation    6 1
# 2    business    6 1
# 3     friends    6 1
# 4  recreation    7 2
# 5    business    7 1
# 6     friends    7 1
# 7  recreation    8 2
# 8    business    8 2
# 9     friends    8 1
# 10 recreation    9 2
# 11   business    9 2
# 12    friends    9 1
# 13 recreation   10 2
# 14   business   10 2
# 15    friends   10 1
# 16 recreation   11 2
# 17    friends   11 1

注意

  • 周是开始周、结束周、之间的所有周
  • n 是一个计数(如果 n = 0,则可以省略,例如第 11 周的“业务”被省略,换句话说,a group_by( , by = c("type", "week") 很好,如果有帮助的话)

我尝试过的

问题的棘手部分是处理周的范围,我想不出一个简洁的方法来做到这一点(即没有循环的方法)。

在这次尝试中,我只处理单周列 - 这只是说明性的 - 它不处理周范围,例如

df %>% 
  select(-id, -end_week) %>% 
  mutate(n=1) %>% 
  pivot_wider(names_from = start_week, values_from = n, values_fill = list(n=0)) %>% 
  pivot_longer(`6`:`7`)

# A tibble: 9 x 3
  type       name  value
  <chr>      <chr> <dbl>
1 recreation 6         1
2 recreation 8         0
3 recreation 7         1
4 business   6         1
5 business   8         1
6 business   7         0
7 friends    6         1
8 friends    8         0
9 friends    7         0

请注意,我的尝试毫无用处,因为它根本没有处理周的范围

最佳答案

library(data.table)

setDT(df)

df[,.(type=type,week=seq(start_week,end_week)),by=seq_len(nrow(df))][,.(n=.N),by=.(type,week)][order(week)]
#>           type week n
#>  1: recreation    6 1
#>  2:    friends    6 1
#>  3:   business    6 1
#>  4: recreation    7 2
#>  5:    friends    7 1
#>  6:   business    7 1
#>  7: recreation    8 2
#>  8:   business    8 2
#>  9:    friends    8 1
#> 10: recreation    9 2
#> 11:   business    9 2
#> 12:    friends    9 1
#> 13: recreation   10 2
#> 14:   business   10 2
#> 15:    friends   10 1
#> 16: recreation   11 2
#> 17:    friends   11 1

reprex package 创建于 2020-05-02 (v0.3.0)

关于r - 按范围调整 pivot_wider 的整洁方法(由两列中的值给出)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61559148/

相关文章:

r - 优化 R 中的文件读取

r - 为什么从 OSM/Nominatim 获取的空间数据框无效?

r - Julia 相当于 dplyr 的 bind_cols 和 bind_rows

r - 对于字符向量,是否有等同于 dplyr::between 的东西?

r - 在 R 中传递空索引

r - 在 R 中使用 dplyr 和摘要向每一行添加哈希

r - 在 data.frame 上分配(不同的)列类

r - 在 dplyr 0.6 中对多个以编程方式指定的变量进行分组

r - 从 group1、group2、overlap_count 创建重叠矩阵?

r - 除了names_from和values_from之外,pivot_wider是否还需要任何其他信息