Two data frame with one being my reference
df1<- structure(list(V1 = structure(c(2L, 14L, 8L, 12L, 1L, 3L, 4L, 5L, 6L, 9L, 10L, 16L, 7L, 15L, 11L, 13L),
You can use match
match
match(df2$V1, df1$V1) #[1] 1 9 10 NA NA 16
If you do not want NA and want it as -, you can use ifelse
NA
-
ifelse
i1 <- match(df2$V1, df1$V1) df2$myindex <- ifelse(is.na(i1), "-", i1) df2 # V1 myindex #1 AbC 1 #2 F 9 #3 GI666 10 #4 Dehli - #5 Bangalore - #6 Mumbai 16