I am working with a single dataframe in R containing the following char columns and values.
C1<-c("1","2","3","4","5")
C2<-c("x", "t", "u", "r", "j")
C3<-c("2","5","3","1","4")
C4<-c("3","1","NA", "2","5")
df<-data.frame(C1,C2,C3,C4)
I am trying to write code that will replace values in C3 and C4 as follows:
The initial dataframe looks like this:
The final dataframe should look like this:
I've yet to come up with code (base R or Dplyr) that will accomplish this task. If anyone can lend assistance, I would really appreciate it.
Thanks!
This is a new df that I've tried to manipulate with the code provided by respondents (eg, df[c("C3", "C4")] <- lapply(df[c("C3", "C4")], function(x) df$C2[match(x, df$C1)])).
I am returning all NA's for C3 C4 and cannot understand why. There are matches between C3 and C1.
We can use match
df[c("C3", "C4")] <- lapply(df[c("C3", "C4")], function(x) df$C2[match(x, df$C1)])
I also used match
, but split it up into two different statements to make it more clear what was going on:
# Create sample data
C1<-c("1","2","3","4","5")
C2<-c("x", "t", "u", "r", "j")
C3<-c("2","5","3","1","4")
C4<-c("3","1","NA", "2","5")
df<-data.frame(C1,C2,C3,C4)
# Make replacements
df$C3_mod <- ifelse(is.na(df$C3), df$C3, df$C2[match(df$C3, df$C1)])
df$C4_mod <- ifelse(is.na(df$C4), df$C4, df$C2[match(df$C4, df$C1)])
# View results
df
# C1 C2 C3 C4 C3_mod C4_mod
# 1 1 x 2 3 t u
# 2 2 t 5 1 j x
# 3 3 u 3 NA u <NA>
# 4 4 r 1 2 x t
# 5 5 j 4 5 r j
Using match
with matrix.
cols <- c('C3', 'C4')
df[cols] <- df$C2[match(as.matrix(df[cols]), df$C1)]
df
# C1 C2 C3 C4
#1 1 x t u
#2 2 t j x
#3 3 u u <NA>
#4 4 r x t
#5 5 j r j
I solved the issue of my NA values. It turns out that I had whitespaces in the column values that I hadn't accounted for. Again, thanks to everyone for their responses. I learned a lot in the process.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.