[R] Shift all values above certain value by 1

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Fri Jan 22 17:38:03 CET 2016


I think I got it:

set.seed(123)
x <- data.frame(a = 1:10, b = 2:11, c = 3:12, other = rnorm(10))
x
temp <- as.matrix(x[1:3])
temp[temp %in% 7] <- 4
temp[temp > 7] <- temp[temp > 7]-1
x[1:3] <- temp
x

It works only with matrices, right? Can't do x[x>7] when x is a data frame?
Thanks!

On Fri, Jan 22, 2016 at 11:34 AM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote:
> Hello!
>
> # I have a data frame x:
> x <- data.frame(a = 1:10, b = 2:11, c = 3:12, other = rnorm(10))
>
> # First, I need to change every value 7 in columns a:c to 4
> # Then, I need to decrease by 1 all values in columns a:c that are >7
>
> What would be the fastest way of doing it?
> Thank you!
>
> --
> Dimitri Liakhovitski



-- 
Dimitri Liakhovitski



More information about the R-help mailing list