- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 assign a column value in a data frame based on another column in another R data frame?
To assign a column value based on another column, we can use ifelse function. The ifelse function checks whether the value in one column of one data frame matches the value in another column of another data frame by using equal sign (==) and then replace the original value with the new column if there is no match else returns the original value. Check out the below example to understand how it can be done.
Example
Consider the below data frame −
> x1<-rpois(20,2) > x2<-rpois(20,5) > df1<-data.frame(x1,x2) > df1
Output
x1 x2 1 3 5 2 3 7 3 0 6 4 0 5 5 4 6 6 3 8 7 1 5 8 0 8 9 4 6 10 1 2 11 2 3 12 2 6 13 0 0 14 1 9 15 0 0 16 4 2 17 3 5 18 3 8 19 1 3 20 2 7
Example
> y1<-rpois(20,2) > y2<-rpois(20,5) > df2<-data.frame(y1,y2) > df2
Output
y1 y2 1 3 8 2 4 11 3 3 8 4 2 2 5 1 3 6 1 4 7 1 7 8 1 2 9 5 2 10 2 4 11 2 3 12 1 3 13 1 5 14 3 4 15 0 3 16 2 5 17 3 5 18 1 7 19 5 10 20 2 6
Replacing values in x1 of df1, if x2 of df1 is same as y2 of df2 otherwise returning x1 in df1 −
> df1$x1<-ifelse(df1$x2==df2$y2,df2$y2,df1$x1) > df1
Output
x1 x2 1 3 5 2 3 7 3 0 6 4 0 5 5 4 6 6 3 8 7 1 5 8 0 8 9 4 6 10 1 2 11 3 3 12 2 6 13 0 0 14 1 9 15 0 0 16 4 2 17 5 5 18 3 8 19 1 3 20 2 7
- Related Articles
- How to extract a data frame’s column value based on a column value of another data frame in R?
- How to find the position of a data frame’s column value based on a column value of another data frame in R?
- How to match a column in a data frame with a column in another data frame in R?
- How to create a new column in an R data frame based on some condition of another column?
- How to find the column means of a column based on another column values that represent factor in an R data frame?
- How to repeat column values in R data frame by values in another column?
- How to repeat a column of a data frame and join it with another data frame in R by rows?
- How to find the frequency of a particular string in a column based on another column in an R data frame using dplyr package?
- How to get row index based on a value of an R data frame column?
- How to extract a particular value based on index from an R data frame column?
- How to subset rows that contains maximum depending on another column in R data frame?
- How to add a new column in an R data frame with count based on factor column?
- Replace numerical column values based on character column values in R data frame.
- How to remove rows containing missing value based on a particular column in an R data frame?
- How to remove rows from data frame in R based on grouping value of a particular column?

Advertisements