Several factors same levels

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Several factors same levels

dereksloan
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.

e.g.

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

falcohol<-factor(alcohol,levels=0:1)
levels<-c("No","Yes")
fsmoker<-factor(smoker,levels=0:1)
levels<-c("No","Yes")

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)?

Derek
Reply | Threaded
Open this post in threaded view
|

Re: Several factors same levels

D. Rizopoulos
one way is the following:

DF <- data.frame(id = 1:6,
     sex = gl(2, 3, labels = c("M", "F")),
     x = sample(0:1, 6, TRUE), y = sample(0:1, 6, TRUE),
     z = rnorm(6))

f <- function (x) {
     if (all(unique(x) %in% 0:1))
         factor(x, levels = 0:1, labels = c("No", "Yes"))
     else
         x
}

DF[] <- lapply(DF, f)


I hope it helps.

Best,
Dimitris


On 4/19/2011 10:57 AM, 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.
>
> e.g.
>
> 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
>
> falcohol<-factor(alcohol,levels=0:1)
> levels<-c("No","Yes")
> fsmoker<-factor(smoker,levels=0:1)
> levels<-c("No","Yes")
>
> 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)?
>
> Derek
>
> --
> 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.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Several factors same levels

Peter Dalgaard-2
In reply to this post by dereksloan

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.
>
> e.g.
>
> 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
>
> falcohol<-factor(alcohol,levels=0:1)
> levels<-c("No","Yes")
> fsmoker<-factor(smoker,levels=0:1)
> levels<-c("No","Yes")
>
> 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]
...
)

>
> Derek
>
> --
> 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.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.