首页 > 解决方案 > 如何在没有错误的情况下做出 if-else 语句?

问题描述

我必须做一个 if 语句,但它说错误。我有一个名为cancercode. 我希望不等于下面所述代码的代码在数据框中声明为“其他”。

df6 <- df4x%>%
mutate(sygdomsgruppex = ifelse(cancercode != "DC911", "DC833", "DC830", 
                             "DC910", "DC921", "DC831", "DC859","DC919", "DC911B",
                             "Other", cancercode))

这段代码给了我这个错误:

mutate_impl(.data, dots) 中的错误:
评估错误:未使用的参数(“DC910”、“DC921”、“DC831”、“DC859”、“DC919”、“DC911B”、“other”、cancerKode)。

标签: rif-statementdplyr

解决方案


other_vector <- c("DC911", "DC833", "DC830", "DC910", "DC921", "DC831", "DC859","DC919", "DC911B")
df4x%>%
  mutate(sygdomsgruppex = ifelse(cancercode %in% other_vector, "Other", cancercode))

推荐阅读