Hi, I'm quite new using R and have got no one to help me get through it. Hopefully someone can help me with one problem I've been struggling with for the last hours!! (Sorry if I'm using the wrong terminology as well!) I have a data matrix in which SIE is one of my variables. What I need to do now is to create a new variable (group) if a certain condition in SIE is met. I tried: if (data2$SIE < 50.646){ data2$group=as.factor("small") } which gives me "In if (data2$SIE < 50.646) { : the condition has length > 1 and only the first element will be used" and ifelse (data2$SIE < 50.64593, data2$group = as.factor("small"),data2$group = as.factor("large")) with "Error: unexpected '=' in "ifelse (data2$SIE < 50.64593, data2$group =" I tried all other kinds of variations and so far, nothing!! Is there a good way to do this that someone could teach me? Thank you so much, Vit?ria
Vit?ria Magalh?es Piai wrote:> Hi, > > I'm quite new using R and have got no one to help me get through it. > Hopefully someone can help me with one problem I've been struggling with > for the last hours!! > (Sorry if I'm using the wrong terminology as well!) > > I have a data matrix in which SIE is one of my variables. What I need to > do now is to create a new variable (group) if a certain condition in SIE > is met. > I tried: > > if (data2$SIE < 50.646){ > data2$group=as.factor("small") > } > which gives me "In if (data2$SIE < 50.646) { : the condition has length > > 1 and only the first element will be used" > > and > ifelse (data2$SIE < 50.64593, data2$group = > as.factor("small"),data2$group = as.factor("large")) > with "Error: unexpected '=' in "ifelse (data2$SIE < 50.64593, > data2$group =" > > I tried all other kinds of variations and so far, nothing!!Do all of the following: ?"=" ?"==" ?"if" ?ifelse -Peter Ehlers> > Is there a good way to do this that someone could teach me? > Thank you so much, > Vit?ria > > ______________________________________________ > R-help at r-project.org 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 Nov 27, 2009, at 5:34 AM, Vit?ria Magalh?es Piai wrote:> Hi, > > I'm quite new using R and have got no one to help me get through it. > Hopefully someone can help me with one problem I've been struggling > with for the last hours!! > (Sorry if I'm using the wrong terminology as well!) > > I have a data matrix in which SIE is one of my variables. What I > need to do now is to create a new variable (group) if a certain > condition in SIE is met. > I tried: > > if (data2$SIE < 50.646){ > data2$group=as.factor("small") > } > which gives me "In if (data2$SIE < 50.646) { : the condition has > length > 1 and only the first element will be used" >If ( cond) is not vectorised. Ifelse is.> and > ifelse (data2$SIE < 50.64593, data2$group = > as.factor("small"),data2$group = as.factor("large")) > with "Error: unexpected '=' in "ifelse (data2$SIE < 50.64593, > data2$group ="Almost... try: data2$group <- ifelse (data2$SIE < 50.64593, as.factor("small"), as.factor("large") ) ... but that looks wrong to my wetware R interpreter, too, so could also try: data2$group <- factor( ifelse (data2$SIE < 50.64593, "small", "large" ) (And of course this would have been tested had you supplied a small test dataset, but this week I am being passive aggressive toward persons who don't follow the Posting Guide.) -- David> > I tried all other kinds of variations and so far, nothing!! > > Is there a good way to do this that someone could teach me? > Thank you so much, > Vit?ria > > ______________________________________________ > R-help at r-project.org 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.David Winsemius, MD Heritage Laboratories West Hartford, CT