如何在R中创建一个带有新列输出的标志

时间:2017-08-07 13:26:35

标签: r data-manipulation

我有以下数据框

all <- c("a","a","a","a","a","a","a","d","d","d","c","c","c","c")

我想创建一个标记数据集中条目的列,因为输出应该在另一列中,让我们说所有$ call与每个a的数据组都标记为好,d为更好,c为最佳

任何人都可以告诉我如何在r编程中这样做。提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用case_when功能。

假设您的条目是向量:

entry <- c("a","a","a","a","a","a","a","d","d","d","c","c","c","c")

然后您可以使用以下命令在新向量中获得所需的输出:

output <- case_when(
  entry == 'a' ~ "good",
  entry == 'd' ~ "better",
  entry == 'c' ~ "best"
)

您可以在https://www.rdocumentation.org/packages/dplyr/versions/0.7.2/topics/case_when

找到有关case_when的更多信息