简体   繁体   English

在 R dplyr 中,根据其在另一列中的值重命名一个值

[英]In R dplyr, rename a value based on its value in another column

I am sure this has been asked before but would really appreciate any help as I am a bit confused.我确信之前有人问过这个问题,但我真的很感激任何帮助,因为我有点困惑。

In R, I have a dataframe with rows for plants' Latin names and their common names.在 R 中,我有一个 dataframe,其中包含植物的拉丁名称及其通用名称的行。 Using dplyr, I want to change the common name if the Latin name equals something specific.使用 dplyr,如果拉丁名称等于特定名称,我想更改通用名称。

ie if Latin_name == 'xyz', rename Common_name to 'abc'即如果 Latin_name == 'xyz',将 Common_name 重命名为 'abc'

I think I know how to do it with the mutate() function, but this adds an entire new column - I'd prefer to rename the values in the original column.我想我知道如何使用 mutate() function 来做到这一点,但这会添加一个全新的列——我更愿意重命名原始列中的值。 Any help would be great, thank you so much.任何帮助都会很棒,非常感谢。

With dplyr :使用dplyr

your_data %>%
  mutate(Common_name = case_when(
    Latin_name == 'xyz' ~ 'abc',
    TRUE ~ Common_name)
  )

In base R:在基地 R 中:

your_data[your_data$Latin_name == 'xyz', 'Common_name'] <- 'abc'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM