You can keep it a dataframe as follows:

set.seed(123)

x <- data.frame(a = 1:10, b = 2:11, c = 3:12, other = rnorm(10))

x

set.seed(123)

x <- data.frame(a = 1:10, b = 2:11, c = 3:12, other = rnorm(10))

x

1 1 2 3 -0.56047565

2 2 3 4 -0.23017749

3 3 4 5 1.55870831

4 4 5 6 0.07050839

5 5 6 7 0.12928774

6 6 7 8 1.71506499

7 7 8 9 0.46091621

8 8 9 10 -1.26506123

9 9 10 11 -0.68685285

10 10 11 12 -0.44566197

# change the columns in the dataframe

x[1:3] <- lapply(x[1:3], function(a){

+ a[a==7] <- 4 # replace valuesx[1:3] <- lapply(x[1:3], function(a){

+ a[a>7] <- a[a>7] - 1 # decrement

+ a # return value

+ })

x

a b c other1 1 2 3 -0.56047565

2 2 3 4 -0.23017749

3 3 4 5 1.55870831

4 4 5 6 0.07050839

5 5 6 4 0.12928774

6 6 4 7 1.71506499

7 4 7 8 0.46091621

8 7 8 9 -1.26506123

9 8 9 10 -0.68685285

10 9 10 11 -0.44566197

?

Jim Holtman

Data Munger Guru

What is the problem that you are trying to solve?

Tell me what you want to do, not how you want to do it.

On Fri, Jan 22, 2016 at 11:38 AM, Dimitri Liakhovitski wrote:

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

wrote:

--

Dimitri Liakhovitski

______________________________________________

R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see

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.

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

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

# 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

______________________________________________

R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see

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.

[[alternative HTML version deleted]]