Mathew, Abraham T
2010-Feb-03 23:36 UTC
[R] Changing an unordered factor into an ordered factor
I'm trying to change an unordered factor into an ordered factor: data96$RV961327 <- data96$V961327 data96$RV961327[data96$V961327 %in% levels(data96$V961327)[4]] <- NA data96$RV961327[data96$V961327 %in% levels(data96$V961327)[5]] <- NA data96$RV961327 <- factor(data96$RV961327) attributes(data96$RV961327) levels(data96$RV961327) data96$RV961327 data96$RRV961327 <- ordered(data96$RV961327, levels=c("Oppose", "Haven't thought much about this", "Favor")) attributes(data96$RRV961327) levels(data96$RRV961327) data96$RRV961327 cbind(data96$V961327, data96$RV961327, data96$RRV961327) table(data96$V961327, data96$RV961327) table(data96$V961327, data96$RRV961327) The first block of code works. However, when I run the ordered command, it only turns all the values into NA's. According to attributes, however, they have been transfered into an ordered factor....but still show up as NA's. Help
Oliver Gondring
2010-Feb-04 10:58 UTC
[R] Changing an unordered factor into an ordered factor
Hi Mathew,> I'm trying to change an unordered factor into an ordered factor: > Helpit's all about reordering the factor levels, right? Have a look at this one: http://rwiki.sciviews.org/doku.php?id=tips:data-factors:factors The third textbox on the page (search for the string: "Reorder factor levels") provides a piece of sample code for the task. Should be easy to adapt it to your problem. Oliver P.S.: Maybe it's not a bad idea to provide a more readable transformation of your code next time. What about replacing the whole 'V961327' thing by 'x' for example?