[R] Several factors same levels
pdalgd at gmail.com
Tue Apr 19 14:13:58 CEST 2011
On Apr 19, 2011, at 10:57 , dereksloan wrote:
> This is probably very simple but I'm new to R so apologies for being stupid.
> I have some data with No coded as 0 and yes coded as 1.
> id sex alcohol smoker
> 1 M 0 1
> 2 F 1 0
> 3 M 0 0
> I realise I can covert the numerical variable back to a factor by
> but can I do this for all factors using the same levels in a single command
> to make it easier (i.e. switch all 0/1, no/yes variables without having to
> recode the same factor levels for them all)?
Yes, if they are in the same data frame. The idiom goes roughly like this
ix <- c("alcohol", "smoker")
new <- paste("f", ix, sep="")
dd[new] <- lapply(dd[ix], factor, levels=0:1, labels=c("No","Yes"))
(You might store the transformed variables back where they came from, just make sure to do it exactly once! That would avoid creating new variable names and give you a bit more flexibility in the choice of index type. There are also various possibilities to actually compute the indices, e.g.
isBin <- function(x) all(na.omit(x) %in% 0:1)
ixl <- sapply(dd, isBin)
ix <- names(dd)[ixl]
> View this message in context: http://r.789695.n4.nabble.com/Several-factors-same-levels-tp3459878p3459878.html
> Sent from the R help mailing list archive at Nabble.com.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help