ropensci / qualtRics

Download ⬇️ Qualtrics survey data directly into R!
https://docs.ropensci.org/qualtRics
Other
215 stars 70 forks source link

Support non-ordered factors when converting data #241

Open b1azk0 opened 3 years ago

b1azk0 commented 3 years ago

Hi, is it possible to pass a ordered = FALSE flag to fetch_survey() along with convert = T, label = T?

The reason I'm asking is, that I only need labels for my factor columns and it looks as if ordering is not working for some reason with my survey.

Variables that are originally 7 level (properly recoded on qualtrics side) produce errors

Warning: Problem with `mutate()` column `qol8`.
ℹ `qol8 = readr::parse_factor(qol8, levels = ln, ordered = TRUE)`.
ℹ 7 parsing failures.
row col           expected                                 actual
  1  -- value in level set bardzo niezadowolony(a)               
  2  -- value in level set raczej niezadowolony(a)               
  3  -- value in level set niezadowolony(a)                      
  5  -- value in level set bardzo zadowolony(a)                  
  6  -- value in level set ani zadowolony(a) ani niezadowolony(a)
... ... .................. ......................................
See problems(...) for more details.

and get imported as NAs only:

> df$qol8
 [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
attr(,"problems")
# A tibble: 7 × 4
    row   col expected           actual                                
  <int> <int> <chr>              <chr>                                 
1     1    NA value in level set bardzo niezadowolony(a)               
2     2    NA value in level set raczej niezadowolony(a)               
3     3    NA value in level set niezadowolony(a)                      
4     5    NA value in level set bardzo zadowolony(a)                  
5     6    NA value in level set ani zadowolony(a) ani niezadowolony(a)
6     9    NA value in level set bardzo niezadowolony(a)               
7    10    NA value in level set bardzo zadowolony(a)                  
attr(,"label")
                                                              qol8 
Kontakty z dziećmi (prosimy o wypełnienie, jeśli Pana(i) dotyczy): 
8 Levels: [1] bardzo niezadowolony(a) < [2] niezadowolony(a)  < ... < <NA>

While, actually there should be responses:

df<-fetch_survey("SV_6fj5q4AwIjhOU0m", time_zone = "Europe/Berlin", force_request = T, start_date = "2021-11-01", convert = F, label = F, include_display_order = F)

df$qol8
 [1]  1  3  2 NA  7  4 NA NA  1  7
attr(,"label")
                                                                qol8 
"Kontakty z dziećmi (prosimy o wypełnienie, jeśli Pana(i) dotyczy):"
juliasilge commented 3 years ago

I think what you want is label = TRUE, convert = FALSE:

library(qualtRics)
library(tidyverse)

sourdough <- fetch_survey("SV_5BJRo2RGHajIlOB", label = TRUE, convert = FALSE, force_request = TRUE)
#>   |                                                                              |                                                                      |   0%  |                                                                              |======================================================================| 100%
#> 
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#>   .default = col_character(),
#>   StartDate = col_datetime(format = ""),
#>   EndDate = col_datetime(format = ""),
#>   Progress = col_double(),
#>   `Duration (in seconds)` = col_double(),
#>   Finished = col_logical(),
#>   RecordedDate = col_datetime(format = ""),
#>   RecipientLastName = col_logical(),
#>   RecipientFirstName = col_logical(),
#>   RecipientEmail = col_logical(),
#>   ExternalReference = col_logical(),
#>   LocationLatitude = col_double(),
#>   LocationLongitude = col_double(),
#>   Q1007 = col_double(),
#>   Q1_DO_1 = col_double(),
#>   Q1_DO_2 = col_double(),
#>   Q1_DO_3 = col_double(),
#>   Q1_DO_4 = col_double(),
#>   Q1_DO_5 = col_double(),
#>   SolutionRevision = col_double(),
#>   FL_6_DO_FL_7 = col_double()
#>   # ... with 4 more columns
#> )
#> ℹ Use `spec()` for the full column specifications.

# this would be ordered otherwise
sourdough %>% count(Q1002)
#> # A tibble: 8 × 2
#>   Q1002                                                                      n
#>   <chr>                                                                  <int>
#> 1 Associate degree in college (2-year)                                      11
#> 2 Bachelor's degree in college (4-year)                                     15
#> 3 Doctoral degree                                                           19
#> 4 High school graduate (high school diploma or equivalent including GED)    16
#> 5 Less than high school degree                                              12
#> 6 Master's degree                                                           20
#> 7 Professional degree (JD, MD)                                              17
#> 8 Some college but no degree                                                15

Created on 2021-11-09 by the reprex package (v2.0.1)

That should get you labels but not try to convert them to an ordered factor. It will not be a factor, but rather a character; if you want to convert it to an unordered factor, you could do that afterwards.

b1azk0 commented 3 years ago

Thank you @juliasilge . I thought there is a way to do it at once - convert to factor without ordering in a single step at import.

juliasilge commented 3 years ago

I don't believe currently. We have this longstanding comment about adding support for something like ordered = FALSE:

https://github.com/ropensci/qualtRics/blob/6e64d2c7d49d113a86a63f9f44fd6eb9f129fc83/R/utils.R#L486

But it is not currently possible. You can either do the ordered factor based on your choiceText from the survey or a character.