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?
changing a range of values
6 messages · Jörg Groß, Dimitris Rizopoulos, jim holtman +3 more
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
Is this what you want:
x <- c(1,1,1,2,2,3,4,4,5) ifelse(x >= 1 & x <= 3, 1, x)
[1] 1 1 1 1 1 1 4 4 5
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.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
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.
The R Inferno, page 38. Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of "The R Inferno" and "A Guide for the Unwilling S User")
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.
x <- c(1,1,1,2,2,3,4,4,5)
> x[x %in% 1:3] [1] 1 1 1 2 2 3 So ... > x[x %in% 1:3] <- 1 > x [1] 1 1 1 1 1 1 4 4 5
On Jan 17, 2009, at 12:03 PM, 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?
It appears to be the result of with argument recycling. If you change
the problem to one where the length of x is not a multiple of the
other side of the equality, you get a warning that is typical for that
sort of problem:
> x <- c(1,1,1,2,2,3,3,3,4,4,5)
>
>
>
> x[x == c(1:3)]
[1] 1 2 3
Warning message:
In x == c(1:3) :
longer object length is not a multiple of shorter object length
> x[x == c(1:3)] <- c(1)
Warning message:
In x == c(1:3) :
longer object length is not a multiple of shorter object length
>
> x
[1] 1 1 1 2 1 1 3 3 4 4 5
I think it is testing these equalities with the matches having a up-
caret.
1 1 1 2 2 3 3 3 4 4 5
1 2 3 1 2 3 1 2 3 1 2
^ ^
David Winsemius > > > ______________________________________________ > 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.