[R] changing a range of values

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jan 17 18:28:54 CET 2009


If we know they are integer values only then this returns
the desired vector

replace(x, x %in% 1:3, 1)

or this overwrites the input:

x[x %in% 1:3] <- 1

On Sat, Jan 17, 2009 at 12:03 PM, Jörg Groß <joerg at licht-malerei.de> wrote:
> Hi,
>
>
> If I have following vector;
>
>
> x <- c(1,1,1,2,2,3,4,4,5)
>
> and I want to change values in the range of 1 to 3 into the value 1, how can
> I do that?
>
>
> I tried
>
>
> x[x == c(1:3)] <- c(1)
>
>
> but than I get;
>
>  x
> [1] 1 1 1 2 1 1 4 4 5
>
>
>
> R doesn't change the 2 into a 1.
> But why?
>
> ______________________________________________
> 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