You are currently viewing R – Replace Column Value with Another Column

How to replace the R data frame column value with another column value? I have already created many articles on replacing column values in R and in this article, I will cover how to replace a column value with another column in an R data frame.

Let’s create an R data frame. If you have NA and want to replace it with 0 use replace NA with 0 in R data frame.


# Create DataFrame
df <- data.frame(id=c(1,2,3,NA),
      address=c('Orange St','Anton Blvd','Jefferson Pkwy',''),
      work_address=c('Main St',NA,'Apple Blvd','Portola Pkwy'))

df
# Output
#  id        address work_address
#1  1      Orange St      Main St
#2  2     Anton Blvd         <NA>
#3  3 Jefferson Pkwy   Apple Blvd
#4 NA                Portola Pkwy

2. Update Column from Another Column

Let’s say you wanted to apply some calculation on the existing column and replace the result with the same column can be achieved with the below R example. Here I am multiplying the column id with the number 5 and assigning the result to the same id column. Similarly, you can also assign the result to another column.

To replace characters in a string, I have covered a dedicated article with a few examples.


# Create new column from existing column
df['id'] <- df['id'] * 5
df

Yields below output.


# Output
  id        address work_address
1  5      Orange St      Main St
2 10     Anton Blvd         <NA>
3 15 Jefferson Pkwy   Apple Blvd
4 NA                Portola Pkwy

3. Replace Value with Another Column by Checking Condition

In case you want to replace a column value for a specific value, you need to check with the condition and assign the value from another column to this column when the condition matches. The below example replaces the address column with the value of work_address when only if the address value is 'Orange St'.


# Replace column value with another based on condition
df$address[df$address == 'Orange St'] <- df$work_address
df

Yields below output.


# Output
  id        address work_address
1  5        Main St      Main St
2 10     Anton Blvd         <NA>
3 15 Jefferson Pkwy   Apple Blvd
4 NA                Portola Pkwy

4. Using dplyr Package

dplyr is a third-party package hence, you need to load the library using library("dplyr") to use its methods. In case you don’t have this package, install it using install.packages("dplyr").

For bigger data sets it is best to use the methods from the dplyr package as they perform 30% faster. This package uses C++ code to evaluate.


# Load library
library(dplyr)    
df <- df %>% 
   mutate(address = ifelse(address == '',work_address,address))
df

Yields below output. Here, %>% is an infix operator which acts as a pipe, it passes the left-hand side of the operator to the first argument of the right-hand side of the operator.


# Output
  id        address work_address
1  5        Main St      Main St
2 10     Anton Blvd         <NA>
3 15 Jefferson Pkwy   Apple Blvd
4 NA   Portola Pkwy Portola Pkwy

5. Replace Column with Another Column

If you want to replace the entire column with another column use the below approach in R data frame. You can also achieve this in the best approach by just renaming the column name and dropping the unwanted column. The below example replaces the values from the work_address column to the address column.


# Replace one column with anohter column
df['address'] <- df['work_address']
df

Yields below output.


# Output
  id      address work_address
1  5      Main St      Main St
2 10         <NA>         <NA>
3 15   Apple Blvd   Apple Blvd
4 NA Portola Pkwy Portola Pkwy

6. Complete Example


# Create DataFrame
df <- data.frame(id=c(1,2,3,NA),
       address=c('Orange St','Anton Blvd','Jefferson Pkwy',''),
       work_address=c('Main St',NA,'Apple Blvd','Portola Pkwy'))
df

# Create new column from existing column
df['id'] <- df['id'] * 5
df

# Example 1
#Replace column value with another based on condition
df$address[df$address == 'Orange St'] <- df$work_address
df

# Replace one column with anohter column
df['address'] <- df['work_address']
df

# Using dplyr package
library(dplyr)    
df <- df %>% 
   mutate(address = ifelse(address == '',work_address,address))
df

# Replace one column with anohter column
df['address'] <- df['work_address']
df

7. Conclusion

In this article, I have explained how to replace an R data frame column value with another column from the same data frame by using several examples.

References