I’ve loaded data from a CSV file into a data frame. Each column represents a survey question, and all of the answers are on a five-point Likert scale, with the labels: (“None”, “Low”, “Medium”, “High”, “Very High”).
When I read in the data initially, R correctly interprets those values as factors but doesn’t know what the ordering should be. I want to specify what the ordering is for the values so I can do some numerical calculations. I thought the following code would work:
X <- read.csv('..')
likerts <- data.frame(apply(X, 2, function(X){factor(X,
levels = c("None", "Low", "Medium", "High", "Very High"),
ordered = T)}))
What happens instead is that all of the level data gets converted into strings. How do I do this correctly?
And the obligatory
plyrsolution (using Joris’s example above):Note that one good thing about
catcolwiseis that it will only apply it to the columns of X that are factors, leaving the others alone. To explain what is going on:catcolwiseis a function that takes a function as an argument, and returns a function that operates “columnwise” on the factor-columns of the data-frame. So we can imagine the above line in two stages:fn <- catcolwise(...); Y <- fn(X). Note that there are also functionscolwise(operates on all columns) andnumcolwise(operate only on numerical columns).