I have a table shown below
data <- data.frame(A = c(1,2,3,4,5),
B = c(1,2,3,4,5),
C = c(11,12,13,14,15),
trans_A = c(NA,1,2,3,4),
trans_B = c(NA,1,2,3,4),
trans_C = c(NA,11,12,13,14))
I need to create/add multiple new columns to this dataframe with new name as difference_A
, difference_B
, etc. with the logic difference_A = A - trans_A
, difference_B = B - trans_B
, etc. How can I do this in R?
I tried this:
new_df[paste("difference", cols, sep = "_")]<- c(NA, new_df[cols]-new_df[paste("trans",cols,sep = "_")], na.rm=TRUE)
it gave me error message "duplicate subscripts for columns"