4 ms·
Strange that someone who knows about plyr (and is clearly not a beginning R user) is still using "for" loops.
by psychometry 12y ago
Strange that someone who knows about plyr (and is clearly not a beginning R user) is still using "for" loops.
- houshuang 12y agoThere are lot's of ways of doing things in R, and I am always learning. I always use vectorized functions when I do something to every row in a data.frame, but when I want to do something to every column, like here, I sometimes use for. How would you use something like plyr here, and would it be as easy to understand? likertcat <- c("1"="Not at all", "2"="To a small extent", "3"="To some extent", "4"="To a moderate extent", "5"="To a large extent") for(e in names(db[,9:44])) { db[[e]] <- revalue(db[[e]], likertcat) db[[e]] <- ordered(db[[e]], levels= c("Not at all","To a small extent", "To some extent","To a moderate extent","To a large extent")) }
- nograpes 12y agoLike this: db<-data.frame(replicate(44,1:5)) likertcat <- as.ordered(c("Not at all", "To a small extent", "To some extent", "To a moderate extent", "To a large extent")) db[ ,9:44] <- lapply(db[,9:44], function(x) likertcat[x])
- houshuang 12y agoThank you. As said, there are always many ways of doing things in R.
- SixSigma 12y agoAs an aside, that is not a valid Likert set. It is not balanced around a central non-comittal value.