是否有根据另一列的条件替换列中特定值的函数?

Is there a function to replace specific values in a column based on condition from another column?

我想用第'Manner'列中所有包含'kala'、'kalla'的单词替换第'Filename'列中的现值(前两个首字母表示扬声器)从“Sonorant”到“Liquid”。

我的想法是,它可以通过 'dplyr'(可能是 'grep')执行,但不知道如何执行。这可以在 MS Excel 中完成,但手动完成会花费大量时间。

任何帮助都会很棒。提前致谢。

这里是我的一些数据:

Filename Consonant           Place   Manner
9   AK_kala.TextGrid Singleton Dental/alveolar Sonorant
10 AK_kalla.TextGrid  Geminate Dental/alveolar Sonorant
14 AK_manni.TextGrid  Geminate Dental/alveolar Sonorant
15  AK_rami.TextGrid Singleton        Bilabial Sonorant
16 AK_rammi.TextGrid  Geminate        Bilabial Sonorant
20  AK_sune.TextGrid Singleton Dental/alveolar Sonorant

您可以检查 Filename 列中的模式并将 Manner 列变为 'Liquid'

df$Manner[grep('kall?a', df$Filename)] <- 'Liquid'
#Maybe this is more clear
#df$Manner[grep('kala|kalla', df$Filename)] <- 'Liquid'
df

#            Filename Consonant           Place   Manner
#9   AK_kala.TextGrid Singleton Dental/alveolar   Liquid
#10 AK_kalla.TextGrid  Geminate Dental/alveolar   Liquid
#14 AK_manni.TextGrid  Geminate Dental/alveolar Sonorant
#15  AK_rami.TextGrid Singleton        Bilabial Sonorant
#16 AK_rammi.TextGrid  Geminate        Bilabial Sonorant
#20  AK_sune.TextGrid Singleton Dental/alveolar Sonorant
library(tidyverse)
my_data <- tibble::tribble(
            ~Filename,  ~Consonant,            ~Place,    ~Manner,
   "AK_kala.TextGrid", "Singleton", "Dental/alveolar", "Sonorant",
  "AK_kalla.TextGrid",  "Geminate", "Dental/alveolar", "Sonorant",
  "AK_manni.TextGrid",  "Geminate", "Dental/alveolar", "Sonorant",
   "AK_rami.TextGrid", "Singleton",        "Bilabial", "Sonorant",
  "AK_rammi.TextGrid",  "Geminate",        "Bilabial", "Sonorant",
   "AK_sune.TextGrid", "Singleton", "Dental/alveolar", "Sonorant"
  )


my_data %>%
  mutate(Manner = if_else(
    Filename %>% str_detect("kala|kalla"),
    "Liquid",
    Manner
  ))