The surveydata package makes it easy to extract specific questions from a surveydata object. Because survey data typically has question names like "Q1_a", "Q1_b", "Q1_c" the extract method for a surveydata object makes it easy to extract all columns by simply specifying "Q1" as the argument to the column index.

# S3 method for surveydata
[(x, i, j, drop = FALSE)

# S3 method for surveydata
[(x, i, j) <- value

# S3 method for surveydata
$(x, name) <- value

Arguments

x

surveydata object

i

row index

j

column index

drop

logical. Passed to [.data.frame. Note that the default is FALSE.

value

New value

name

Names of columns

Details

Extraction is similar to data frames, with three important exceptions:

  • The column argument j is evaluated using which.q() and will return all columns where the column names match the pattern().

  • The drop argument is FALSE. Thus the result will always be a surveydata object, even if only a single column is returned.

  • All extraction methods retain the pattern and varlabels arguments.

Examples


names(membersurvey)
#>   [1] "id"        "Q1_1"      "Q1_2"      "Q2"        "Q3_1"      "Q3_2"     
#>   [7] "Q3_3"      "Q3_4"      "Q3_5"      "Q3_6"      "Q3_7"      "Q3_8"     
#>  [13] "Q3_9"      "Q3_10"     "Q3_11"     "Q3_12"     "Q3_13"     "Q3_14"    
#>  [19] "Q3_15"     "Q4"        "Q5"        "Q6_1"      "Q6_2"      "Q6_3"     
#>  [25] "Q6_4"      "Q6_5"      "Q6_6"      "Q6_7"      "Q6_8"      "Q6_9"     
#>  [31] "Q6_10"     "Q7"        "Q8"        "Q9_1"      "Q9_2"      "Q9_3"     
#>  [37] "Q9_4"      "Q9_5"      "Q10"       "Q11_1"     "Q11_2"     "Q11_3"    
#>  [43] "Q11_4"     "Q11_5"     "Q11_other" "Q12_1"     "Q13_1"     "Q13_2"    
#>  [49] "Q13_3"     "Q13_4"     "Q14_1"     "Q14_2"     "Q14_3"     "Q14_4"    
#>  [55] "Q14_5"     "Q14_6"     "Q14_7"     "Q14_8"     "Q14_9"     "Q14_10"   
#>  [61] "Q15_1"     "Q15_2"     "Q15_3"     "Q15_4"     "Q15_5"     "Q15_6"    
#>  [67] "Q19_1"     "Q19_2"     "Q19_3"     "Q19_4"     "Q19_5"     "Q19_6"    
#>  [73] "Q20_1"     "Q20_2"     "Q20_3"     "Q20_4"     "Q20_5"     "Q20_6"    
#>  [79] "Q20_7"     "Q20_8"     "Q20_9"     "Q20_10"    "Q20_other" "Q21_1"    
#>  [85] "Q21_2"     "Q21_3"     "Q21_4"     "Q21_5"     "Q21_6"     "Q23_1"    
#>  [91] "Q23_2"     "Q23_3"     "Q23_4"     "Q23_5"     "Q23_other" "Q24"      
#>  [97] "Q25"       "Q26_1"     "Q27_1"     "Q27_2"     "Q30"       "Q30_other"
#> [103] "Q31"       "Q31_other" "Q32"       "Q33"       "Q35"       "weight"   
#> [109] "size"     
head(membersurvey["Q1"])
#>   Q1_1 Q1_2
#> 1    8    2
#> 2   35   12
#> 3   34   12
#> 4   20    9
#> 5   20    3
#> 6   36   20
head(membersurvey[c("Q1", "Q2")])
#>   Q1_1 Q1_2          Q2
#> 1    8    2        2009
#> 2   35   12 Before 2002
#> 3   34   12 Before 2002
#> 4   20    9        2010
#> 5   20    3        2010
#> 6   36   20 Before 2002
head(membersurvey[membersurvey$Q2=="2009", c("Q1", "Q2")])
#>    Q1_1 Q1_2   Q2
#> 1     8  2.0 2009
#> 7    12  2.5 2009
#> 16   20  5.0 2009
#> 37   25  2.0 2009
#> 51    7  2.5 2009
#> 65    7 20.0 2009

# The pattern is used to extract columns

pattern(membersurvey)
#> $sep
#> [1] "_"
#> 
#> $exclude
#> [1] "other"
#> 

grep("Q20", names(membersurvey), value=TRUE)
#>  [1] "Q20_1"     "Q20_2"     "Q20_3"     "Q20_4"     "Q20_5"     "Q20_6"    
#>  [7] "Q20_7"     "Q20_8"     "Q20_9"     "Q20_10"    "Q20_other"
head(membersurvey["Q20"])
#>   Q20_1 Q20_2 Q20_3 Q20_4 Q20_5 Q20_6 Q20_7 Q20_8 Q20_9 Q20_10
#> 1   Yes    No    No    No    No    No    No    No    No     No
#> 2    No    No    No    No    No    No    No    No    No     No
#> 3    No    No    No    No    No    No    No    No    No     No
#> 4   Yes   Yes    No    No    No    No    No    No    No    Yes
#> 5    No    No    No    No    No    No    No    No    No     No
#> 6    No   Yes    No   Yes   Yes    No    No    No    No     No
head(membersurvey["Q20_other"])
#>                                        Q20_other
#> 1                                           <NA>
#> 2                                           <NA>
#> 3                                           <NA>
#> 4                                           <NA>
#> 5 I rarely attend business events in the evening
#> 6                                           <NA>