R – Replace Column Value with Another Column

  • Post author:

How to replace R DataFrame columns value with or from another column? 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 DataFrame.

Let’s create an R DataFrame. If you NA and wanted to replace it with 0 use replace NA with 0 in R DataFrame.


# 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 to the same column can be achieved with the below R example. Here I am multiplying column id with 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 wanted 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 matched. The below example replaces the address column with the value of work_address when only if 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 dplyr package as they perform 30% faster. dplyr 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 wanted to replace the entire column with another column use the below approach in R DataFrame. 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 DataFrame column value with another column from the same DataFrame by using several examples.

Related Articles

References

r replace another column

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

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