in dataframe df have likert-scale items coded 0-4. after importing these raw file r, stored characters. created list boolean indicating whether each variable character, used lapply change factor.
i <- sapply(df, is.character) df[i] <- lapply(df[i], as.factor)
when looking @ of factor variables (all coded 0-4), get:
df$reactance1 [1] 3 3 4 3 2 4 1 4 3 3 1 1 4 3 4 3 4 4 levels: 1 2 3 4 3 4
or
df$eai4 [1] 0 0 2 1 2 0 3 4 1 3 0 0 0 0 0 0 1 0 levels: 0 1 2 3 4 0 1 2
i tried solve problem droplevels()
df$reactance1 <- droplevels(df$reactance1)
my guess r, reason, thinks character 3 different character 3, although should treated identically. results in many levels. checked raw data see if there maybe spaces in front of of numbers, not seem it.
thanks in advance help!
this case of leading/lagging
spaces in 'character' columns. remove spaces trimws
(base r
function) if class
of column character
and convert them factor
or else
leave column such.
df[] <- lapply(df, function(x) if(is.character(x)){ factor(trimws(x)) } else x )
Comments
Post a Comment