Мы можем использовать substr
, чтобы получить первые 2 символа в Key
и получить соответствующее значение из dictonary
.
df$New_column <- dictonary[substr(df$Key, 1, 2)]
df
# No. Region Key Value_X Percent Year New_column
#1 1 X 0201062 5020101062 0% 2015 Name1
#2 2 Y 0211032 5020211032 0% 2015 Name1
#3 3 Z 0216043 5020216043 0% 2015 Name1
#4 4 U 0415082 6040615082 0% 2015 Name2
#5 5 K 0401062 6040801062 0% 2015 Name2
#6 6 M 0601102 3060901102 0% 2015 Name3
data
df <- structure(list(No. = c("1", "2", "3", "4", "5", "6"), Region = c("X",
"Y", "Z", "U", "K", "M"), Key = c("0201062", "0211032", "0216043",
"0415082", "0401062", "0601102"), Value_X = c("5020101062", "5020211032",
"5020216043", "6040615082", "6040801062", "3060901102"), Percent = c("0%",
"0%", "0%", "0%", "0%", "0%"), Year = c("2015", "2015", "2015",
"2015", "2015", "2015")), row.names = c(NA, -6L), class = "data.frame")