0

I am having problems replacing values in a column i a table with values from another table/list. I have the following example dataset 'df':

type    col2    col3
A       NA      blue
A       America blue
A       Nam     blue
B       NA      red
B       ''      red
B       'Nam'   red

And I have this simplified table, with the temperatures for each value in column 'type':

type    temp
A       hot
B       cold

So, I am trying to replace and/or fill (if na=TRUE) the values for the initial data.frame (df$type) with the correspondent data from list and obtain this result:

type    new_Col2    col3
A       hot         blue
A       hot         blue
A       hot         blue
B       cold        red
B       cold        red
B       cold        red

I used the normal commands for replacing/fill:

df <- df %>% 
        mutate(type = replace(type, type == 'A', 'hot'),
        type = replace(type, type == 'B', 'cold'))

However, my real data have +100.000 rows (and 15,000 different 'types'), and I am stuck trying to solve how I can do the replacement recursively.

The problem is that the length of each data.frame is different, so I can't replace each value using the 'mutate/replace' commands.

Someone can help me to solve this?

Any help would be appreciated. Thanks!

  • 1
    Create a key/value data and then do a join i.e. `keydat <- tibble(type = c("A", "B"), val = c("hot", "cold")); df %>% left_join(keydat, by = c("type")) %>% mutate(type = coalesce(val, type))` – akrun Jul 12 '22 at 17:57
  • 1
    As akrun said, this really is a merge/join operation. See https://stackoverflow.com/q/1299871/3358272 and https://stackoverflow.com/q/5706437/3358272 – r2evans Jul 12 '22 at 18:00

1 Answers1

2

Why don't you use left_join?

df <- df1 %>%
 left_join(df2, by="type")
Léo Henry
  • 127
  • 10