How to change a column in an R data frame with some conditions?

R ProgrammingServer Side ProgrammingProgramming

Sometimes, the column value of a particular column has some relation with another column and we might need to change the value of that particular column based on some conditions. We need to make this change to check how the change in the values of a column can make an impact on the relationship between the two columns under consideration. In R, we can use single square brackets to make the changes in the column values.

Example

Consider the below data frame −

> set.seed(1)
> x1<-rpois(20,5)
> x2<-rpois(20,2)
> x3<-runif(20,2,5)
> df<-data.frame(x1,x2,x3)
> df
x1 x2 x3
1 4 4 4.462839
2 4 1 3.941181
3 5 2 4.348798
4 8 0 3.659109
5 3 1 3.589159
6 8 1 4.368069
7 9 0 2.069994
8 6 1 3.431690
9 6 4 4.196941
10 2 1 4.078195
11 3 2 3.432859
12 3 2 4.583628
13 6 2 3.314291
14 4 1 2.734392
15 7 3 2.212037
16 5 2 2.298398
17 6 3 2.948815
18 11 0 3.555903
19 4 3 3.986015
20 7 2 3.220491

Suppose we want to subtract 2 from column 2 (x2) values if the column 3 values are greater than 3, then it can be done as shown below −

> df$x2[df$x3 > 3] <- (df$x2[df$x3 > 3] - 2)
> df
x1 x2 x3
1 4 -2.375000 4.462839
2 4 -2.562500 3.941181
3 5 -2.400000 4.348798
4 8 -2.281250 3.659109
5 3 -2.777778 3.589159
6 8 -2.265625 4.368069
7 9 0.000000 2.069994
8 6 -2.234568 3.431690
9 6 -2.277778 4.196941
10 2 -2.361111 4.078195
11 3 -3.000000 3.432859
12 3 -2.666667 4.583628
13 6 -2.666667 3.314291
14 4 1.000000 2.734392
15 7 3.000000 2.212037
16 5 2.000000 2.298398
17 6 3.000000 2.948815
18 11 -2.388889 3.555903
19 4 -2.437500 3.986015
20 7 -2.285714 3.220491

If we want to multiple column 1 (x1) values to 2 for the values where x3 is less than 3 then it can be done as shown below −

> df$x1[df$x3 < 3] <- (df$x1[df$x3 < 3]*2)
> df
x1 x2 x3
1 4 -1.0937500 4.462839
2 4 -1.1406250 3.941181
3 5 -0.8800000 4.348798
4 8 -0.5351562 3.659109
5 3 -1.5925926 3.589159
6 8 -0.5332031 4.368069
7 18 0.0000000 2.069994
8 6 -0.4705075 3.431690
9 6 -0.7129630 4.196941
10 2 -0.7268519 4.078195
11 3 -2.5000000 3.432859
12 3 -1.5555556 4.583628
13 6 -1.5555556 3.314291
14 8 1.0000000 2.734392
15 14 3.0000000 2.212037
16 10 2.0000000 2.298398
17 12 3.0000000 2.948815
18 11 -0.7314815 3.555903
19 4 -1.1093750 3.986015
20 7 -0.6122449 3.220491
raja
Published on 11-Aug-2020 13:46:04
Advertisements