The pattern attribute contains information about the separator character used to name sub-questions in the data. Survey software typically makes use of underscores to distinguish sub-questions in a grid of questions, e.g. "Q4_1", "Q4_2", "Q4_3", "Q4_other". The function pattern() returns the pattern attribute, and pattern<- updates the attribute.

pattern(x)

pattern(x) <- value

Arguments

x

surveydata object

value

New value

See also

as.surveydata(), which.q()

Other Attribute functions: varlabels()

Examples

# Extract the pattern from membersurvey

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

# The pattern is used to extract columns

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

# Define a new pattern

pattern(membersurvey) <- list(sep="_", exclude="")
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
#>                                        Q20_other
#> 1                                           <NA>
#> 2                                           <NA>
#> 3                                           <NA>
#> 4                                           <NA>
#> 5 I rarely attend business events in the evening
#> 6                                           <NA>

# Reset original pattern

pattern(membersurvey) <- oldptn
rm(oldptn)