Changing selected columns to factor

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

Changing selected columns to factor

reichmaj
R-Help Forum

 

If I have a data frame consisting of say ten (10) variables
(A,B,C,D,E,F,G,H,I,J) and I want to change Variables 2,7,8,and 9 to factors
is there a command such that I can do it in one line or do I simply have to
convert each separately?

 

Jeff


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Changing selected columns to factor

Jim Lemon-4
Hi Jeff,

jrdf<-data.frame(A=rnorm(10),B=rnorm(10),C=rnorm(10),
 D=rnorm(10),E=rnorm(10),F=rnorm(10),G=rnorm(10),
 H=rnorm(10),I=rnorm(10),J=rnorm(10))
for(i in c(2,7,8,9)) jrdf[,i]<-factor(jrdf[,i])
sapply(jrdf,"class")

Jim

On Tue, Jun 12, 2018 at 9:57 AM, Jeff Reichman <[hidden email]> wrote:

> R-Help Forum
>
>
>
> If I have a data frame consisting of say ten (10) variables
> (A,B,C,D,E,F,G,H,I,J) and I want to change Variables 2,7,8,and 9 to factors
> is there a command such that I can do it in one line or do I simply have to
> convert each separately?
>
>
>
> Jeff
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Changing selected columns to factor

reichmaj
Well that’s easy enough - thank you

-----Original Message-----
From: Jim Lemon <[hidden email]>
Sent: Monday, June 11, 2018 11:50 PM
To: Jeff Reichman <[hidden email]>
Cc: r-help mailing list <[hidden email]>
Subject: Re: [R] Changing selected columns to factor

Hi Jeff,

jrdf<-data.frame(A=rnorm(10),B=rnorm(10),C=rnorm(10),
 D=rnorm(10),E=rnorm(10),F=rnorm(10),G=rnorm(10),
 H=rnorm(10),I=rnorm(10),J=rnorm(10))
for(i in c(2,7,8,9)) jrdf[,i]<-factor(jrdf[,i])
sapply(jrdf,"class")

Jim

On Tue, Jun 12, 2018 at 9:57 AM, Jeff Reichman <[hidden email]> wrote:

> R-Help Forum
>
>
>
> If I have a data frame consisting of say ten (10) variables
> (A,B,C,D,E,F,G,H,I,J) and I want to change Variables 2,7,8,and 9 to
> factors is there a command such that I can do it in one line or do I
> simply have to convert each separately?
>
>
>
> Jeff
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.