I want to change names in a dataframe if these names occur in another second dataframe. However, I want to change these names to the names that occur in the second column of the second dataframe.

# first data frame
words = c("Hello", "Hello", "Hello", "Hello", "Hoi", "Hoi", "Hoi", "Goodday", "Goodday", 
          "Goodday", "Goodday", "Goodday", "bye", "bye", "bye", "bye", "bye", "see you", "see you", "see you", "see you")
numbers = c(1:21)
df = data.frame(words, numbers)

# second data frame
words1 = c("Hello", "Hoi", "Goodday", "bye", "see you")
clusters = c("greeting", "greeting", "both", "farewell", "farewell")
df2 = data.frame(words1 , clusters)

# if loop
if (df$words %in% df2$words1){
  df$words <- df2$clusters
}

I would want something like this:

test    numbers
greeting    1
greeting    2
greeting    3
greeting    4
greeting    5
greeting    6
greeting    7
both    8
both    9
both    10
both    11
both    12
farewell    13
farewell    14
farewell    15
farewell    16
farewell    17
farewell    18
farewell    19
farewell    20
farewell    21

0 Answers