根据data.frame中的(组)值扩展data.frame

时间:2019-11-25 19:37:44

标签: r dplyr tidyr purrr

可以说我有以下数据框:

tibble(user = c('A', 'B'), first = c(1,4), last = c(6, 9))
# A tibble: 2 x 3
  user  first  last
  <chr> <dbl> <dbl>
1 A         1     6
2 B         4     9

并想要创建一个类似于以下内容的小标题

bind_rows(tibble(user = 'A', weeks = 1:6), 
          tibble(user = 'B', weeks = 4:9))
# A tibble: 12 x 2
   user  weeks
   <chr> <int>
 1 A         1
 2 A         2
 3 A         3
 4 A         4
 5 A         5
 6 A         6
 7 B         4
 8 B         5
 9 B         6
10 B         7
11 B         8
12 B         9

我该怎么做呢?我尝试过:

tibble(user = c('A', 'B'), first = c(1,4), last = c(6, 9)) %>% 
group_by(user) %>% 
mutate(weeks = first:last)

我想知道是否应该尝试将complete mapnest组合使用?

2 个答案:

答案 0 :(得分:2)

创建序列后,一个选项是unnest

library(dplyr)
library(purrr)
df1 %>% 
  transmute(user, weeks = map2(first, last, `:`)) %>%
  unnest(weeks)
# A tibble: 12 x 2
#   user  weeks
#   <chr> <int>
# 1 A         1
# 2 A         2
# 3 A         3
# 4 A         4
# 5 A         5
# 6 A         6
# 7 B         4
# 8 B         5
# 9 B         6
#10 B         7
#11 B         8
#12 B         9

或者另一个选择是rowwise

df1 %>% 
  rowwise %>%
  transmute(user, weeks = list(first:last)) %>% 
  unnest(weeks)

或者没有任何包装

stack(setNames(Map(`:`, df1$first, df1$last), df1$user))

或以其他方式写为

stack(setNames(do.call(Map, c(f = `:`, df1[-1])), df1$user))

数据

df1 <- tibble(user = c('A', 'B'), first = c(1,4), last = c(6, 9))

答案 1 :(得分:1)

涉及dplyrtidyr的一个选项可能是:

df %>%
 uncount(last - first + 1) %>%
 group_by(user) %>%
 transmute(weeks = first + 1:n() - 1)

   user  weeks
   <chr> <dbl>
 1 A         1
 2 A         2
 3 A         3
 4 A         4
 5 A         5
 6 A         6
 7 B         4
 8 B         5
 9 B         6
10 B         7
11 B         8
12 B         9