It would be easier to do with a key/val dataset and a join which can be fast and efficient
library(data.table)
keydat <- data.table(state = c("TX", "CAL", "PEN"), CVE_GEO = c("45", "50", "56"))
setDT(df)[keydat, CVE := i.CVE_GEO, on = .(state)]
-output
df
# year state CVE
#1: 1980 TX 45
#2: 1986 PEN 56
#3: 1987 CAL 50
#4: 2000 TX 45
NOTE: Here we assume that the CVE
column in the original dataset is of the same type as the 'keydat' 'CVE_GEO' i.e. character
class
data
df <- structure(list(year = c(1980L, 1986L, 1987L, 2000L), state = c("TX",
"PEN", "CAL", "TX"), CVE = c("3", "3", "3", "3")), row.names = c(NA,
-4L), class = "data.frame")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…