Created
May 9, 2013 02:07
-
-
Save houshuang/5545067 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Reproducible example | |
lik <- structure(list(Relevant = structure(c(6L, 5L, 6L, 2L, 5L, 4L, 5L, 5L, 6L, 2L, 6L, 3L, 5L, 4L, 5L, 3L, 2L, 2L, 4L, NA, 2L, 6L, NA, 5L, 5L, 5L, 5L, 5L, 6L, 6L), .Label = c("0", "Agree", "Disagree", "Neutral", "Strongly Agree", "Strongly Disagree"), class = "factor"), Help.career = structure(c(3L, 5L, 6L, 2L, 5L, 2L, 5L, 5L, 6L, 2L, 6L, 3L, 5L, 4L, 3L, 2L, 2L, 2L, 2L, NA, 6L, 6L, 2L, 5L, 5L, 5L, 5L, 4L, 6L, 6L), .Label = c("0", "Agree", "Disagree", "Neutral", "Strongly Agree", "Strongly Disagree"), class = "factor"), Earn.credential = structure(c(6L, 5L, 2L, 2L, 2L, 4L, 5L, 5L, 6L, 4L, 6L, 2L, 5L, 2L, 3L, 3L, 2L, 4L, 5L, NA, 3L, 6L, NA, 6L, 3L, 4L, 4L, 6L, 6L, 2L), .Label = c("0", "Agree", "Disagree", "Neutral", "Strongly Agree", "Strongly Disagree"), class = "factor"), Prestigious.U = structure(c(6L, 5L, 2L, 2L, 2L, 4L, 5L, 2L, 6L, 3L, 6L, 4L, 5L, 4L, 3L, 3L, 2L, 4L, 2L, NA, 6L, 5L, NA, 2L, 3L, 5L, 2L, 5L, 6L, 6L), .Label = c("0", "Agree", "Disagree", "Neutral", "Strongly Agree", "Strongly Disagree"), class = "factor"), Fun = structure(c(5L, 5L, 2L, 5L, 5L, 5L, 5L, 2L, 5L, 5L, 5L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L, 5L, 5L, 2L, 5L, 5L, 5L, 2L, 2L, 6L, 5L), .Label = c("0", "Agree", "Disagree", "Neutral", "Strongly Agree", "Strongly Disagree"), class = "factor"), usercat = structure(c(7L, 5L, 4L, 3L, 5L, 3L, 5L, 7L, 3L, 3L, 5L, 7L, 7L, 3L, 5L, 5L, 3L, 3L, 3L, 2L, 3L, 5L, 3L, 5L, 3L, 7L, 3L, 3L, 4L, 3L), .Label = c("", "Academic / Professor", "Lifelong Learner", "None of the above", "Professional", "Researcher", "Student (University/College)"), class = "factor")), .Names = c("Relevant", "Help.career", "Earn.credential", "Prestigious.U", "Fun", "usercat"), row.names = c(NA, 30L), class = "data.frame") | |
reshape.likert <- function(x, factors=c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"), columns="ALL") { | |
row.names(x) <- NULL | |
if (columns=="ALL") { cols <- x} else { cols <- x[,columns] } # choose relevant columns | |
cols <- sapply(cols, function(y) table(factor(y))) # get summary counts for each line | |
cols <- t(cols) # flip the axis | |
#cols <- cols[,factors] # resort the factors - this gives an error if not data.frame | |
return(cols) | |
} | |
#Why do these two give totally different answers (different data structures)? | |
reshape.likert(lik[,c(1:4)]) | |
reshape.likert(lik[,c(1:5)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment