[R] categorical column to numeric column

Stephen Tucker brown_emu at yahoo.com
Mon Feb 19 15:26:01 CET 2007


try

dd$g <- ifelse(dd$aa=="a",1,-1)

and in general, you can convert categorical data (factors) into integers with
as.integer(), though the values will be positive:

dd$f <- as.integer(factor(dd$aa))


--- Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:

> Hi R,
> 
>  
> 
> Let 'dd' be a data frame given as:
> 
>  
> 
> dd=data.frame(aa=c("a","a","b","a","b","b"),bb=c(1,1,1,2,3,4))
> 
>  
> 
> Now I want to create a column 'g' such that if dd$aa=a then dd$g=1 else
> dd$g= -1 .
> 
>  
> 
> So, I gave the below syntax:
> 
>  
> 
> if((dd$aa)=="a") dd$g=1 else dd$g= -1
> 
>  
> 
> But I get the error message as:
> 
> Warning message: 
> 
> the condition has length > 1 and only the first element will be used in:
> if ((dd$aa) == "a") dd$g = 1 else dd$g = -1 
> 
>  
> 
> and dd=
> 
>  
> 
> > dd
> 
>   aa bb g
> 
> 1  a  1 1
> 
> 2  a  1 1
> 
> 3  b  1 1
> 
> 4  a  2 1
> 
> 5  b  3 1
> 
> 6  b  4 1
> 
> > 
> 
>  
> 
> Please let me know what is the error I am doing?
> 
>  
> 
>  
> 
>  
> 
>  
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 




 
____________________________________________________________________________________
TV dinner still cooling? 
Check out "Tonight's Picks" on Yahoo! TV.



More information about the R-help mailing list