How to generate a new column and fill the values if the particular columns values in CSV A and CSV B match?

Viewed 56

I am working on R Programming, where there are 2 CSV files containing the below data.

My program should work if the values of Shipping_ID(CSV_A) and Customer_ID(CSV_B) matches. The CSV_A should create an extra column named 'New Column' and that should fill the data of 'Previous status'(CSV_B) to the 'New Column' in CSV_A. Finally, CSV_A is to be exported as CSV_A in a different location to my system.

The issue with my current script: The issue is the below script only matches with the columns and gives me the results as a list.

Could someone help me with how to do this in R?

CSV_A:

S.No.   Shipping_ID   Current Status
1          50         Shipped
2          30         Shipped but not delivered
3          40         In progress
4          10         Shipped
5          20         Not Shipped

CSV_B:

S.No.   Customer_ID    Previous Status
1         10           Shipping in progress
2         20           Shipping in progress
3         30           Shipped

Expected Result as CSV_A

S. No.  Shipping_ID         Current Status                    New Column
1           50                 shipped  
2           30                 Shipped but not delivered        Shipped
3           40                 in progress  
4           10                 Shipped                          Shipping in progress
5           20                 Not Shipped                      Shipping in progress

R SCript

library(SASxport)

CSV_A <- 'C:/Users/Userid/Desktop/csv/CSV_A.csv'
CSV_B <- 'C:/Users/Userid/Desktop/csv/CSV_B.csv'


library(tidyverse)
CSV_A<-read.csv(CSV_A )
CSV_A
CSV_B<-read.csv(CSV_B)
CSV_B
CSV_A$Shipping_ID<- CSV_B$Customer_ID[match(CSV_A$Shipping_ID, CSV_B$Customer_ID)]
1 Answers

Try changing the second column name of CSV_B

   colnames(CSV_B)[2] <- "Shipping_ID"

Then join dataframes,

library(dplyr)
left_join(CSV_A, CSV_B, by = Shipping_ID)
Related