Replace each value in a column with the largest value based on a condition in R data frame.


Suppose we have three columns say X, Y, and Z in an R data frame called df and we want to replace values in columns X and Y with the same value if the values are greater than values in Z and if they are less than the values in Z then we can replace with Z values.

Check out the below Examples to understand how it can be done.

Example 1

Following snippet creates a sample data frame −

x1<-rpois(20,2)
x2<-rpois(20,2)
X<-rpois(20,3)
df1<-data.frame(x1,x2,X)
df1

The following dataframe is created

 x1 x2 X
 1 1 1 3
 2 2 2 2
 3 1 0 2
 4 2 4 0
 5 3 3 4
 6 4 1 1
 7 1 3 2
 8 4 2 2
 9 4 3 3
10 0 0 2
11 1 4 4
12 2 2 3
13 2 2 1
14 2 1 1
15 3 2 4
16 0 5 1
17 0 0 2
18 1 1 5
19 1 1 3
20 4 2 2

To replace values in x1 and x2 with X if they are less than X on the above created data frame, add the following code to the above snippet −

x1<-rpois(20,2)
x2<-rpois(20,2)
X<-rpois(20,3)
df1<-data.frame(x1,x2,X)
df1[,1:2]<-lapply(df1[,1:2],function(x) ifelse(xdf1$X,x,X))
df1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

 x1 x2 X
 1 3 3 3
 2 2 2 2
 3 2 2 2
 4 2 4 0
 5 4 4 4
 6 4 1 1
 7 2 3 2
 8 4 2 2
 9 4 3 3
10 2 2 2
11 4 4 4
12 3 3 3
13 2 2 1
14 2 1 1
15 4 4 4
16 1 5 1
17 2 2 2
18 5 5 5
19 3 3 3
20 4 2 2

Example 2

Following snippet creates a sample data frame −

y1<-rnorm(20,0.5)
y2<-rnorm(20,0.5)
Y<-rnorm(20,0.5,0.005)
df2<-data.frame(y1,y2,Y)
df2

The following dataframe is created

          y1        y2         Y
 1  0.38903537  2.2068051 0.5082834
 2  0.08703056  1.4083968 0.5056107
 3  0.37278616  1.1038885 0.4990037
 4  0.80350925  1.9704348 0.5038784
 5  0.97611860  1.2431881 0.5008880
 6 -0.61610694  0.7217463 0.4940775
 7 -0.37572148  2.6484561 0.5016710
 8  1.61492508 -0.8688714 0.5046321
 9  0.58858684 -0.7627633 0.5020626
10  2.45589388  0.5280574 0.4963291
11  0.79954296  0.3476735 0.4998230
12  0.32497257 -1.1904892 0.4979471
13 -1.00420011 -1.4069050 0.5007824
14  3.57048593  1.2896375 0.4987264
15  2.26500575  1.5387301 0.5037580
16  1.58224909 -0.9105896 0.4838976
17 -0.31704488 -0.1611695 0.4917168
18  0.89396005 -1.0374997 0.4976947
19 -0.18188955  0.2133147 0.4915311
20  1.27196146 -0.7174173 0.4922855

To replace values in y1 and y2 with Y if they are less than Y on the above created data frame, add the following code to the above snippet −

y1<-rnorm(20,0.5)
y2<-rnorm(20,0.5)
Y<-rnorm(20,0.5,0.005)
df2<-data.frame(y1,y2,Y)
df2[,1:2]<-lapply(df2[,1:2],function(x) ifelse(xdf2$Y,x,Y))
df2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

         y1        y2        Y
 1 0.5082834 2.2068051 0.5082834
 2 0.5056107 1.4083968 0.5056107
 3 0.4990037 1.1038885 0.4990037
 4 0.8035093 1.9704348 0.5038784
 5 0.9761186 1.2431881 0.5008880
 6 0.4940775 0.7217463 0.4940775
 7 0.5016710 2.6484561 0.5016710
 8 1.6149251 0.5046321 0.5046321
 9 0.5885868 0.5020626 0.5020626
10 2.4558939 0.5280574 0.4963291
11 0.7995430 0.4998230 0.4998230
12 0.4979471 0.4979471 0.4979471
13 0.5007824 0.5007824 0.5007824
14 3.5704859 1.2896375 0.4987264
15 2.2650058 1.5387301 0.5037580
16 1.5822491 0.4838976 0.4838976
17 0.4917168 0.4917168 0.4917168
18 0.8939600 0.4976947 0.4976947
19 0.4915311 0.4915311 0.4915311
20 1.2719615 0.4922855 0.4922855

Updated on: 10-Nov-2021

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements