[R] If condition using accessors
David Winsemius
dwinsemius at comcast.net
Fri Nov 27 15:46:58 CET 2009
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
More information about the R-help
mailing list