如何替换数据框中的特定单词

K.I*_*I.N 2 r

category.1 <- c("TM","TM","CPA","TM","CPC")
category.2 <- c("LS","LS","DSP","DSP","AF")
platform <- c("facebook","facebook","yahoo","google","google")

dat <- data.frame(platform,category.1,category.2)
dat
  platform category.1 category.2
1 facebook         TM         LS
2 facebook         TM         LS
3    yahoo        CPA        DSP
4   google         TM        DSP
5   google        CPC         AF
Run Code Online (Sandbox Code Playgroud)

当category.1是'TM'和category.2'LS'时,我想把'LS'替换为'LS1'

      platform category.1 category.2
    1 facebook         TM         LS1
    2 facebook         TM         LS1
    3    yahoo        CPA        DSP
    4   google         TM        DSP
    5   google        CPC         AF
Run Code Online (Sandbox Code Playgroud)

我试过这种方式,它的返回错误.

 dat$category.1[dat$category.1=='TM'& dat$category.2=='LS',] <- 'LS1'
Run Code Online (Sandbox Code Playgroud)

谢谢你的阅读.

Bar*_*art 6

另一种方法; 使用dplyrifelse基本功能.

> library(dplyr)
> dat <-
    dat %>%
    mutate(category.2 = ifelse(category.1 == "TM" & category.2 == "LS",
           "LS1",
           as.character(category.2)))


> dat
  platform category.1 category.2
1 facebook         TM        LS1
2 facebook         TM        LS1
3    yahoo        CPA        DSP
4   google         TM        DSP
5   google        CPC         AF
Run Code Online (Sandbox Code Playgroud)