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:
14:36
Aan: r-help
Onderwerp: [R] categorical column to numeric column
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
On 2/19/2007 8:36 AM, Shubha Vishwanath Karanth 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:
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 [EMAIL PROTECTED] wrote:
Hi R,
Let 'dd' be a data frame
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 .
You need to use ifelse instead of the if ... else construction:
dd$g = ifelse(dd$a=='a', 1, -1)
cu
Philipp
:Re: [R] categorical column to numeric column
On 2/19/2007 8:36 AM, Shubha Vishwanath Karanth 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