[R] If Then Trouble
Uwe Ligges
ligges at statistik.tu-dortmund.de
Sun Apr 24 22:21:16 CEST 2011
On 24.04.2011 22:10, Sparks, John James wrote:
> Dear R Helpers,
>
> I have another one of those problems involving a very simple step, but due
> to my inexperience I can't find a way to solve it. I had a look at a
> number of on-line references, but they don't speak to this problem.
>
> I have a variable with 20 values
>
>> table (testY2$redgroups)
>
> 1 2 3 4 5 6 7 8 9 10 11 12
> 13 14 15 16 17 18 19 20
> 69 734 6079 18578 13693 6412 3548 1646 659 323 129 88
> 90 40 57 33 36 17 6 13
>
> Values 18,19 and 20 have small counts. So, I want to set the value of
> redgroups for these rows to 17 in order to combine groups. I would think
> that it would be as easy as
>
> if(testY2$redgroups>17) testY2$redgroups<-17
>
> following the syntax that I have seen in the manuals. However, I get the
> error message
>
> Warning message:
> In if (testY2$redgroups> 17) testY2$redgroups<- 17 :
> the condition has length> 1 and only the first element will be used
>
> Can someone please tell me the correct syntax for this? I would really
> appreciate it.
See help("if") and find if(foo) is only designed for scalar values of
foo. You actually want to use:
if(testY2$redgroups>17) testY2$redgroups<-17
testY2$redgroups[testY2$redgroups > 17] <- 17
or using some less efficient "if"-like statement:
testY2$redgroups <- ifelse(testY2$redgroups > 17, 17, testY2$redgroups)
Uwe Ligges
>
> Appreciatively yours,
> --John J. Sparks, Ph.D.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list