使用pivot_wider()“传播”多个变量

时间:2019-08-01 00:45:58

标签: r tidyr

pivot_wider()的开发版本中,使用tidyr“传播”多个变量的最佳方法是什么?

# https://tidyr.tidyverse.org/dev/reference/pivot_wider.html
# devtools::install_github("tidyverse/tidyr")
library(tidyr)
library(tidyverse)
have <- tibble::tribble(
  ~user_id, ~question, ~answer, ~timestamp,
  1, "q1", "a1", "2019-07-22 16:54:43",
  1, "q2", "a2", "2019-07-22 16:55:43",
  2, "q1", "a1", "2019-07-22 16:56:43",
  2, "q2", "a2", "2019-07-22 16:57:43",
  3, "q1", "a1", "2019-07-22 16:58:43",
  3, "q2", "a2", "2019-07-22 16:59:43"
) %>%
  mutate(timestamp = as_datetime(timestamp))

have
# # A tibble: 6 x 4
# user_id question answer timestamp          
# <dbl> <chr>    <chr>  <dttm>             
#   1       1 q1       a1     2019-07-22 16:54:43
#   2       1 q2       a2     2019-07-22 16:55:43
#   3       2 q1       a1     2019-07-22 16:56:43
#   4       2 q2       a2     2019-07-22 16:57:43
#   5       3 q1       a1     2019-07-22 16:58:43
#   6       3 q2       a2     2019-07-22 16:59:43

want <- tibble::tribble(
    ~user_id, ~q1, ~q2, ~timestamp_q1, ~timestamp_q2,
    1, "a1", "a2", "2019-07-22 16:54:43", "2019-07-22 16:55:43",
    2, "a1", "a2", "2019-07-22 16:56:43", "2019-07-22 16:57:43",
    3, "a1", "a2", "2019-07-22 16:58:43", "2019-07-22 16:59:43"
  ) %>%
  mutate(timestamp_q1 = as_datetime(timestamp_q1)) %>%
  mutate(timestamp_q2 = as_datetime(timestamp_q2))

want
# A tibble: 3 x 5
#  user_id q1    q2    timestamp_q1        timestamp_q2       
#    <dbl> <chr> <chr> <dttm>              <dttm>             
#1       1 a1    a2    2019-07-22 16:54:43 2019-07-22 16:55:43
#2       2 a1    a2    2019-07-22 16:56:43 2019-07-22 16:57:43
#3       3 a1    a2    2019-07-22 16:58:43 2019-07-22 16:59:43

如果您想散布一对变量,则可以使用此方法,但是失败,因为只有user_id应该是标识变量。

have %>%
  pivot_wider(names_from = question, values_from = answer)

# # A tibble: 6 x 4
# user_id timestamp           q1    q2   
# <dbl> <dttm>              <chr> <chr>
#   1       1 2019-07-22 16:54:43 a1    NA   
#   2       1 2019-07-22 16:55:43 NA    a2   
#   3       2 2019-07-22 16:56:43 a1    NA   
#   4       2 2019-07-22 16:57:43 NA    a2   
#   5       3 2019-07-22 16:58:43 a1    NA   
#   6       3 2019-07-22 16:59:43 NA    a2   

1 个答案:

答案 0 :(得分:2)

您可以在values_from参数中包含多个列,以一次性分散多个列:

have %>%
    pivot_wider(
        id_cols = user_id,
        names_from = question,
        values_from = c(answer, timestamp)
    ) %>%
    # remove the 'answer_' prefix from those cols
    rename_all(~ str_remove(., "answer_"))

输出:

# A tibble: 3 x 5
  user_id q1    q2    timestamp_q1        timestamp_q2       
    <dbl> <chr> <chr> <dttm>              <dttm>             
1       1 a1    a2    2019-07-22 16:54:43 2019-07-22 16:55:43
2       2 a1    a2    2019-07-22 16:56:43 2019-07-22 16:57:43
3       3 a1    a2    2019-07-22 16:58:43 2019-07-22 16:59:43