R function to substitute a character with any integer

57 Views Asked by At

I have 2 dataframes with numeric codes that represent various jobs.
One df (df_a) has codes from census the other (df_b) has codes that represent essential jobs.
I need to create a new column in df_a with where jobs are listed as essential or non-essential based on codes in df_b.
The issue is that some job codes in df_a have character M = multiple integers in df_b (e.g. 123M5 in df_a = 12335, 12345, 12355... in df_b). I am trying to accomplish this by setting M='\\d' in df_a but not succeeding... any thoughts on a better way to approach this? Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

This might be what you need:

df_a <- data.frame(job = c("1233M5", "12345", "1235M5" , "12346"))
df_b <- data.frame(essential = c(12335, 12346))

df_a$essential <- 0
df_a$essential[which(as.numeric(gsub("M", "", df_a$job)) %in% df_b$essential)] <- 1