[R] if statement

Sarah Goslee sarah.goslee at gmail.com
Wed Mar 7 20:22:27 CET 2012


You need ifelse() instead of if().

On Wed, Mar 7, 2012 at 2:12 PM, Val <valkremk at gmail.com> wrote:
> Hi All,
>
> I have one difficulty in using the conditional if statement
>
> Assume ,
>
> x <- -1:4
>  x
> [1] -1  0  1  2  3  4
>
> if x is lees than want I want to add 1 and I used the following command
>  if(x<0) {x=x+1}
>
> Warning message:
> In if (x < 0) { :
>  the condition has length > 1 and only the first element will be used
>> x
> [1] 0 1 2 3 4 5
>  That command added 1 to each element.
>
> But I want like this  0 0 1 2 3 4
>
> Can anybody help me?
>
> Thanks
> Val

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list