[R] changing a range of values

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Sat Jan 17 18:10:01 CET 2009


try this:

x <- c(1,1,1,2,2,3,4,4,5,3.2,0.5)

x[x >= 1 & x <= 3] <- 1
x


I hope it helps.

Best,
Dimitris


Jörg Groß 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.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014



More information about the R-help mailing list