R/surveydata-package.R
surveydata-package.Rd
Tools, classes and methods to manipulate survey data.
Surveydata objects have been designed to function with SPSS export data, i.e. the result of an SPSS import, foreign::read.spss()
. This type of data is contained in a data.frame, with information about the questionnaire text in the variable.labels
attribute. Surveydata objects keep track of the variable labels, by offering methods for renaming, subsetting, etc.
Coercion functions:
To access and modify attributes:
To subset or merge surveydata objects:
To extract question text from varlabels:
To fix common encoding problems:
To clean data:
remove_dont_know()
to remove "Don't know" responses
remove_all_dont_know()
to remove "Don't know" responses from all questions
fix_levels_01()
to fix level formatting of all question with Yes/No type answers
Miscellaneous tools:
dropout()
to determine questions where respondents drop out
library(surveydata)
# Create surveydata object
sdat <- data.frame(
id = 1:4,
Q1 = c("Yes", "No", "Yes", "Yes"),
Q4_1 = c(1, 2, 1, 2),
Q4_2 = c(3, 4, 4, 3),
Q4_3 = c(5, 5, 6, 6),
Q10 = factor(c("Male", "Female", "Female", "Male")),
crossbreak = c("A", "A", "B", "B"),
weight = c(0.9, 1.1, 0.8, 1.2)
)
varlabels(sdat) <- c(
"RespID",
"Question 1",
"Question 4: red", "Question 4: green", "Question 4: blue",
"Question 10",
"crossbreak",
"weight"
)
sv <- as.surveydata(sdat, renameVarlabels = TRUE)
# Extract specific questions
sv[, "Q1"]
#> Q1
#> 1 Yes
#> 2 No
#> 3 Yes
#> 4 Yes
sv[, "Q4"]
#> Q4_1 Q4_2 Q4_3
#> 1 1 3 5
#> 2 2 4 5
#> 3 1 4 6
#> 4 2 3 6
# Query attributes
varlabels(sv)
#> id Q1 Q4_1 Q4_2
#> "RespID" "Question 1" "Question 4: red" "Question 4: green"
#> Q4_3 Q10 crossbreak weight
#> "Question 4: blue" "Question 10" "crossbreak" "weight"
pattern(sv)
#> $sep
#> [1] "_"
#>
#> $exclude
#> [1] "other"
#>
# Find unique questions
questions(sv)
#> [1] "id" "Q1" "Q4" "Q10" "crossbreak"
#> [6] "weight"
which.q(sv, "Q1")
#> [1] 2
which.q(sv, "Q4")
#> [1] 3 4 5
# Find question text
question_text(sv, "Q1")
#> [1] "Question 1"
question_text(sv, "Q4")
#> [1] "Question 4: red" "Question 4: green" "Question 4: blue"
question_text_common(sv, "Q4")
#> [1] "Question 4"
question_text_unique(sv, "Q4")
#> [1] "red" "green" "blue"
# Basic operations on a surveydata object, illustrated with the example dataset membersurvey
class(membersurvey)
#> [1] "surveydata" "data.frame"
questions(membersurvey)
#> [1] "id" "Q1" "Q2" "Q3" "Q4" "Q5"
#> [7] "Q6" "Q7" "Q8" "Q9" "Q10" "Q11"
#> [13] "Q11_other" "Q12" "Q13" "Q14" "Q15" "Q19"
#> [19] "Q20" "Q20_other" "Q21" "Q23" "Q23_other" "Q24"
#> [25] "Q25" "Q26" "Q27" "Q30" "Q30_other" "Q31"
#> [31] "Q31_other" "Q32" "Q33" "Q35" "weight" "size"
which.q(membersurvey, "Q1")
#> [1] 2 3
which.q(membersurvey, "Q3")
#> [1] 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
which.q(membersurvey, c("Q1", "Q3"))
#> [1] 2 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
question_text(membersurvey, "Q3")
#> [1] "[Carry out ad hoc qualitative projects] With regard to your market research work, which of these do you do at all?"
#> [2] "[Carry out ad hoc quantitative projects] With regard to your market research work, which of these do you do at all?"
#> [3] "[Design ad hoc projects but don't personally carry them out] With regard to your market research work, which of these do you do at all?"
#> [4] "[Design/advise on research programmes as a whole] With regard to your market research work, which of these do you do at all?"
#> [5] "[Provide project management] With regard to your market research work, which of these do you do at all?"
#> [6] "[Summarise previous research] With regard to your market research work, which of these do you do at all?"
#> [7] "[Carry out desk research] With regard to your market research work, which of these do you do at all?"
#> [8] "[Provide modelling/ statistical services] With regard to your market research work, which of these do you do at all?"
#> [9] "[Provide consultancy on other specific aspects of research] With regard to your market research work, which of these do you do at all?"
#> [10] "[Provide field services] With regard to your market research work, which of these do you do at all?"
#> [11] "[Provide data processing services] With regard to your market research work, which of these do you do at all?"
#> [12] "[Provide other part project services (interviewing, moderating, report writing)] With regard to your market research work, which of these do you do at all?"
#> [13] "[Lecture/teach] With regard to your market research work, which of these do you do at all?"
#> [14] "[Provide training services] With regard to your market research work, which of these do you do at all?"
#> [15] "[Interim management] With regard to your market research work, which of these do you do at all?"
question_text_unique(membersurvey, "Q3")
#> [1] "Carry out ad hoc qualitative projects"
#> [2] "Carry out ad hoc quantitative projects"
#> [3] "Design ad hoc projects but don't personally carry them out"
#> [4] "Design/advise on research programmes as a whole"
#> [5] "Provide project management"
#> [6] "Summarise previous research"
#> [7] "Carry out desk research"
#> [8] "Provide modelling/ statistical services"
#> [9] "Provide consultancy on other specific aspects of research"
#> [10] "Provide field services"
#> [11] "Provide data processing services"
#> [12] "Provide other part project services (interviewing, moderating, report writing)"
#> [13] "Lecture/teach"
#> [14] "Provide training services"
#> [15] "Interim management"
question_text_common(membersurvey, "Q3")
#> [1] "With regard to your market research work, which of these do you do at all?"
# Extracting columns from a surveydata object
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["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[, "Q3"])
#> Q3_1 Q3_2 Q3_3 Q3_4 Q3_5 Q3_6 Q3_7 Q3_8 Q3_9 Q3_10 Q3_11 Q3_12 Q3_13 Q3_14
#> 1 No No No No No No No No No Yes No No No No
#> 2 Yes No No No No No No No Yes No No Yes No No
#> 3 Yes Yes No No No Yes No No No No No No No No
#> 4 No No No No No No No No No No Yes No No No
#> 5 No No No No No No No No No No No No No Yes
#> 6 No Yes No No No No No No Yes No No No No No
#> Q3_15
#> 1 No
#> 2 No
#> 3 No
#> 4 No
#> 5 No
#> 6 No
head(membersurvey[, c("Q1", "Q3")])
#> Q1_1 Q1_2 Q3_1 Q3_2 Q3_3 Q3_4 Q3_5 Q3_6 Q3_7 Q3_8 Q3_9 Q3_10 Q3_11 Q3_12
#> 1 8 2 No No No No No No No No No Yes No No
#> 2 35 12 Yes No No No No No No No Yes No No Yes
#> 3 34 12 Yes Yes No No No Yes No No No No No No
#> 4 20 9 No No No No No No No No No No Yes No
#> 5 20 3 No No No No No No No No No No No No
#> 6 36 20 No Yes No No No No No No Yes No No No
#> Q3_13 Q3_14 Q3_15
#> 1 No No No
#> 2 No No No
#> 3 No No No
#> 4 No No No
#> 5 No Yes No
#> 6 No No No
# Note that the result is always a surveydata object, even if only one column is extracted
head(membersurvey[, "id"])
#> id
#> 1 3
#> 2 5
#> 3 6
#> 4 11
#> 5 13
#> 6 15
str(membersurvey[, "id"])
#> Classes ‘surveydata’ and 'data.frame': 215 obs. of 1 variable:
#> $ id: num 3 5 6 11 13 15 21 22 23 25 ...
#> - attr(*, "variable.labels")= Named chr "id"
#> ..- attr(*, "names")= chr "id"
#> - attr(*, "pattern")=List of 2
#> ..$ sep : chr "_"
#> ..$ exclude: chr "other"