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

Suppose you want to calculate and update an existing column with the result. This can be done using the following R example. In this step, I am multiplying the values in the id column by 5 and storing the result back in the same id column. Likewise, you can store the result in a different column if needed.

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 Condition

To replace column values based on a condition, you must evaluate the condition and assign values from another column when the condition is satisfied. In the example below, the address column is updated with values from the work_address column only when 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. Perform Replacing in R by mutate()

Since dplyr is a third-party package, you need to load it using library("dplyr") to access its methods. If the package isn’t installed yet, you can install it with install.packages("dplyr").

For larger data sets, it is advisable to use methods from the dplyr package because they perform about 30% faster. This efficiency is due to the package utilizing C++ code for evaluation.

Let’s use the mutate() function of this package to replace the column value with another column.


# 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 the 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