# Load the dataframes
dfa <- data.frame(name = c("jack", "rose"),
id = c(1, 2))
dfb <- data.frame(id = c("a", "b"),
job = c("student", "tutor"))
dfc <- data.frame(primary = c("a", "b"),
colname = c(2, 1))
# Obtain the indexes that match between dfa and dfc
index <- match(dfa$id, dfc$colname)
# Set the id from dfa as the id in dfb taking into account the previous indices
dfa$id <- dfb$id[index]
# Print results
dfa
name id
1 jack b
2 rose a
2条答案
按热度按时间db2dz4w81#
一种
dplyr
方法是这样使用recode
:或者第二选项将是使用
left_join
:数据
kh212irz2#
我不明白这三个数据集之间的关系是不是这样的:dfa$id等于dfc$colname,dfc$primary等于dfb$id,但如果是这样,可能的答案如下: