[R] categorical column to numeric column

Philipp Pagel philipp.pagel.lists at t-online.de
Mon Feb 19 15:56:45 CET 2007


> 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

-- 
Dr. Philipp Pagel                            Tel.  +49-8161-71 2131
Dept. of Genome Oriented Bioinformatics      Fax.  +49-8161-71 2186
Technical University of Munich
Science Center Weihenstephan
85350 Freising, Germany

 and

Institute for Bioinformatics / MIPS          Tel.  +49-89-3187 3675
GSF - National Research Center               Fax.  +49-89-3187 3585
      for Environment and Health
Ingolstädter Landstrasse 1
85764 Neuherberg, Germany
http://mips.gsf.de/staff/pagel



More information about the R-help mailing list