如果此名称出现在其他数据框的A列中,则将该数据框中的名称更改为该数据框的B列中的名称

时间:2019-05-10 11:08:34

标签: r dataframe for-loop if-statement

如果这些名称出现在另一个第二数据框中,我想更改数据框中的名称。但是,我想将这些名称更改为第二个数据框的第二列中出现的名称。

# 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
}

我想要这样的东西:

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 个答案:

没有答案