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> ddaa 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]]
Maybe this isn't the most elegant way, but it should work. dd$g <- -1 dd$g[dd$aa == "a"] <- 1 Cheers, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney -----Oorspronkelijk bericht----- Van: r-help-bounces op stat.math.ethz.ch [mailto:r-help-bounces op stat.math.ethz.ch] Namens Shubha Vishwanath Karanth Verzonden: maandag 19 februari 2007 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: 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> ddaa 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 op 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.
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: > > > > if((dd$aa)=="a") dd$g=1 else dd$g= -1if() looks at just the first entry; it's designed for flow of control rather than vectorized calculations. You want ifelse(): ifelse( dd$aa == "a", 1, -1) Duncan Murdoch> > > 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.
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.
> 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
Hi On 19 Feb 2007 at 9:11, Duncan Murdoch wrote: Date sent: Mon, 19 Feb 2007 09:11:52 -0500 From: Duncan Murdoch <murdoch at stats.uwo.ca> To: Shubha Vishwanath Karanth <shubhak at ambaresearch.com> Copies to: r-help <R-help at stat.math.ethz.ch> Subject: 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=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 > > if() looks at just the first entry; it's designed for flow of control > rather than vectorized calculations. You want ifelse(): > > ifelse( dd$aa == "a", 1, -1)Another approach is to use the fact that logical vector can be interpreted as 1 and 0 vector (dd$a=="a")*2-1 HTH Petr> > Duncan Murdoch > > > > > > 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. > > ______________________________________________ > 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.Petr Pikal petr.pikal at precheza.cz