Skip to contents

Works much like tidyr::pivot_longer, but also extracts column labels. Values are always converted to character.

With keep_attributes = TRUE it additionally carries each pivoted column's full attribute set (class, levels, label, the survey metadata) in an attributes list-column, so the operation can be reversed losslessly with srv_pivot_wider() — the pattern used to reshape typed survey data, edit it in long form, then widen it back (e.g. before a Crunch upload).

Usage

srv_pivot_longer(data, cols, keep_attributes = FALSE)

Arguments

data

A data frame

cols

<tidy-select> variables to gather

keep_attributes

Logical. If TRUE, carry an attributes list-column holding each pivoted column's full attribute set and make name a factor in column order. Required to reverse the pivot with srv_pivot_wider(). Defaults to FALSE (label-only, one-way).

Value

A "lengthened" data.frame with new columns in place of the pivoted columns:

  • name - names of the pivoted columns (a factor in column order when keep_attributes = TRUE, otherwise character)

  • label - their labels

  • attributes - (only when keep_attributes = TRUE) a list-column of each pivoted column's attributes, consumed by srv_pivot_wider()

  • response - values, converted to character. In most cases values in survey data come as labelled doubles or factors, so this column keeps the labels.

Details

It is usually a good idea to post-process survey question labels before using them in plots or elsewhere. You may find it convenient to use srv_pivot_longer in combination with srv_extract_split.

See also

srv_pivot_wider() to reverse the operation.

Examples

library(dplyr)
data(ds_survey_example, package = "g6tr.survey")
set.seed(1)
df <- ds_survey_example |>
  mutate(weight = runif(n(), 0.8, 1))

# All labelled variables
df |> srv_pivot_longer(where(haven::is.labelled))
#> # A tibble: 1,950 × 5
#>    uuid             weight name       label                             response
#>    <chr>             <dbl> <chr>      <chr>                             <chr>   
#>  1 2yb2aakzwy9n6uu0  0.853 s_majorr1  s_majorr1: Computer Science - Wh… NA      
#>  2 2yb2aakzwy9n6uu0  0.853 s_majorr2  s_majorr2: Data Science - What i… NA      
#>  3 2yb2aakzwy9n6uu0  0.853 s_majorr3  s_majorr3: Statistics / Mathemat… NA      
#>  4 2yb2aakzwy9n6uu0  0.853 s_majorr4  s_majorr4: Information Technolog… NA      
#>  5 2yb2aakzwy9n6uu0  0.853 s_majorr5  s_majorr5: Business - What is yo… NA      
#>  6 2yb2aakzwy9n6uu0  0.853 s_majorr6  s_majorr6: Engineering - What is… NA      
#>  7 2yb2aakzwy9n6uu0  0.853 s_majorr7  s_majorr7: History - What is you… NA      
#>  8 2yb2aakzwy9n6uu0  0.853 s_majorr8  s_majorr8: English - What is you… NA      
#>  9 2yb2aakzwy9n6uu0  0.853 s_majorr9  s_majorr9: Political Science - W… NA      
#> 10 2yb2aakzwy9n6uu0  0.853 s_majorr10 s_majorr10: Economics - What is … NA      
#> # ℹ 1,940 more rows

# All variables but some
df |> srv_pivot_longer(-c(uuid, weight))
#> # A tibble: 1,950 × 5
#>    uuid             weight name       label                             response
#>    <chr>             <dbl> <chr>      <chr>                             <chr>   
#>  1 2yb2aakzwy9n6uu0  0.853 s_majorr1  s_majorr1: Computer Science - Wh… NA      
#>  2 2yb2aakzwy9n6uu0  0.853 s_majorr2  s_majorr2: Data Science - What i… NA      
#>  3 2yb2aakzwy9n6uu0  0.853 s_majorr3  s_majorr3: Statistics / Mathemat… NA      
#>  4 2yb2aakzwy9n6uu0  0.853 s_majorr4  s_majorr4: Information Technolog… NA      
#>  5 2yb2aakzwy9n6uu0  0.853 s_majorr5  s_majorr5: Business - What is yo… NA      
#>  6 2yb2aakzwy9n6uu0  0.853 s_majorr6  s_majorr6: Engineering - What is… NA      
#>  7 2yb2aakzwy9n6uu0  0.853 s_majorr7  s_majorr7: History - What is you… NA      
#>  8 2yb2aakzwy9n6uu0  0.853 s_majorr8  s_majorr8: English - What is you… NA      
#>  9 2yb2aakzwy9n6uu0  0.853 s_majorr9  s_majorr9: Political Science - W… NA      
#> 10 2yb2aakzwy9n6uu0  0.853 s_majorr10 s_majorr10: Economics - What is … NA      
#> # ℹ 1,940 more rows

# Single question, apply srv_extract_split
df |>
  select(uuid, weight, starts_with("s_major")) |>
  srv_pivot_longer(-c(uuid, weight)) |>
  mutate(label = srv_extract_split(label, sep = ": ")) |>
  mutate(label = srv_extract_split(label, sep = " - ", position = 1))
#> # A tibble: 650 × 5
#>    uuid             weight name       label                       response
#>    <chr>             <dbl> <chr>      <chr>                       <chr>   
#>  1 2yb2aakzwy9n6uu0  0.853 s_majorr1  Computer Science            NA      
#>  2 2yb2aakzwy9n6uu0  0.853 s_majorr2  Data Science                NA      
#>  3 2yb2aakzwy9n6uu0  0.853 s_majorr3  Statistics / Mathematics    NA      
#>  4 2yb2aakzwy9n6uu0  0.853 s_majorr4  Information Technology (IT) NA      
#>  5 2yb2aakzwy9n6uu0  0.853 s_majorr5  Business                    NA      
#>  6 2yb2aakzwy9n6uu0  0.853 s_majorr6  Engineering                 NA      
#>  7 2yb2aakzwy9n6uu0  0.853 s_majorr7  History                     NA      
#>  8 2yb2aakzwy9n6uu0  0.853 s_majorr8  English                     NA      
#>  9 2yb2aakzwy9n6uu0  0.853 s_majorr9  Political Science           NA      
#> 10 2yb2aakzwy9n6uu0  0.853 s_majorr10 Economics                   NA      
#> # ℹ 640 more rows

# Carry attributes (class, levels, label, metadata) so the pivot can be
# reversed losslessly with srv_pivot_wider()
df |>
  select(uuid, starts_with("s_major")) |>
  srv_pivot_longer(-uuid, keep_attributes = TRUE)
#> # A tibble: 650 × 5
#>    uuid             name       label                       attributes   response
#>    <chr>            <fct>      <chr>                       <list>       <chr>   
#>  1 2yb2aakzwy9n6uu0 s_majorr1  s_majorr1: Computer Scienc… <named list> NA      
#>  2 2yb2aakzwy9n6uu0 s_majorr2  s_majorr2: Data Science - … <named list> NA      
#>  3 2yb2aakzwy9n6uu0 s_majorr3  s_majorr3: Statistics / Ma… <named list> NA      
#>  4 2yb2aakzwy9n6uu0 s_majorr4  s_majorr4: Information Tec… <named list> NA      
#>  5 2yb2aakzwy9n6uu0 s_majorr5  s_majorr5: Business - What… <named list> NA      
#>  6 2yb2aakzwy9n6uu0 s_majorr6  s_majorr6: Engineering - W… <named list> NA      
#>  7 2yb2aakzwy9n6uu0 s_majorr7  s_majorr7: History - What … <named list> NA      
#>  8 2yb2aakzwy9n6uu0 s_majorr8  s_majorr8: English - What … <named list> NA      
#>  9 2yb2aakzwy9n6uu0 s_majorr9  s_majorr9: Political Scien… <named list> NA      
#> 10 2yb2aakzwy9n6uu0 s_majorr10 s_majorr10: Economics - Wh… <named list> NA      
#> # ℹ 640 more rows