- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change row values based on column values in an R data frame?
Changing row values based on column values means that we want to change the row values for a particular column if the column values satisfy a certain condition. For example, if we have a data frame called df that contains a column say x and we want to set all the values in x to 5 if they are greater than 5 then it can be done as df[df$x>5,]<-5.
Example1
Consider the below data frame −
> x1<-rpois(20,1) > x2<-rpois(20,5) > df1<-data.frame(x1,x2) > df1
Output
x1 x2 1 3 10 2 3 3 3 1 8 4 2 4 5 1 7 6 1 4 7 2 4 8 0 9 9 2 10 10 2 6 11 1 4 12 1 7 13 1 4 14 1 5 15 2 2 16 4 3 17 3 4 18 0 1 19 0 1 20 0 7
Changing values in x1 to 2 if they are greater than 2 −
> df1[df1$x1>2,]<-2 > df1
Output
x1 x2 1 2 2 2 2 2 3 1 8 4 2 4 5 1 7 6 1 4 7 2 4 8 0 9 9 2 10 10 2 6 11 1 4 12 1 7 13 1 4 14 1 5 15 2 2 16 2 2 17 2 2 18 0 1 19 0 1 20 0 7
Example2
> y1<-rnorm(20,5,1) > y2<-rnorm(20,5,0.3) > df2<-data.frame(y1,y2) > df2
Output
y1 y2 1 4.826729 5.433352 2 3.530581 5.266034 3 5.811061 4.582195 4 5.620806 5.745011 5 4.799159 5.323772 6 5.287042 5.111730 7 5.128602 5.192648 8 4.372339 5.014256 9 5.078192 5.175813 10 4.392499 5.231547 11 4.983643 5.390611 12 4.168723 4.620990 13 2.691008 4.932305 14 3.715511 4.455306 15 3.674058 5.136752 16 5.439690 5.082509 17 6.393444 4.139432 18 5.220288 5.201459 19 4.695000 4.960111 20 5.925427 5.013475
Changing values in y1 to 3 if they are greater than 3 −
> df2[df2$y1>3,]<-3 > df2
Output
y1 y2 1 3.000000 3.000000 2 3.000000 3.000000 3 3.000000 3.000000 4 3.000000 3.000000 5 3.000000 3.000000 6 3.000000 3.000000 7 3.000000 3.000000 8 3.000000 3.000000 9 3.000000 3.000000 10 3.000000 3.000000 11 3.000000 3.000000 12 3.000000 3.000000 13 2.691008 4.932305 14 3.000000 3.000000 15 3.000000 3.000000 16 3.000000 3.000000 17 3.000000 3.000000 18 3.000000 3.000000 19 3.000000 3.000000 20 3.000000 3.000000
- Related Articles
- How to divide row values of a numerical column based on categorical column values in an R data frame?
- Replace numerical column values based on character column values in R data frame.
- How to subset row values based on columns name in R data frame?
- How to select rows based on range of values of a column in an R data frame?
- How to subset rows of an R data frame based on duplicate values in a particular column?
- How to remove rows from an R data frame based on frequency of values in grouping column?
- How to find the sum of values based on key in other column of an R data frame?
- How to get row index based on a value of an R data frame column?
- How to extract columns based on particular column values of an R data frame that match a pattern?
- How to find the column means of a column based on another column values that represent factor in an R data frame?
- How to fill the NA values from above row values in an R data frame?
- How to select positive values in an R data frame column?
- How to randomly replace values in an R data frame column?
- How to fill NA values with previous values in an R data frame column?
- How to select the first and last row based on group column in an R data frame?

Advertisements