简体   繁体   中英

“spread” multiple variables using pivot_wider()

What is the best way to "spread" multiple variables using pivot_wider() in the development version of 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

This works if you want to spread one pair of variables, but fails because only user_id should be the identifying variable.

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   

You can include multiple columns in the values_from argument to spread multiple columns in one go:

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

Output:

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM