从r中的先前列中创建分类变量

发布于 2025-02-09 01:13:08 字数 362 浏览 1 评论 0 原文

我一直在尝试从以前的二进制变量创建一个新的分类变量。我想包括的所有二进制变量(咳嗽,发烧等)都有“ 1'或0”的值。 这是我一直在尝试的代码:

symptoms<-ifelse(cough==1,"cough",ifelse(fever==1,"fever",ifelse(diarrhea==1,"diarrhea",ifelse(dispnea==1,"dispnea",ifelse(neurologic==1,"neurologic",ifelse(otherSymp==1,"other symtomes", NA))))))

问题在于,输出仅为4个类别,而不是6类。

我知道这是一个基本问题,对不起,并提前感谢您。

I have been trying to create a new categorical variable from previous binary variables. All the binary variables I'm trying to include (cough, fever, etc...) have values of either '1' or '0'.
This is the code I've been trying:

symptoms<-ifelse(cough==1,"cough",ifelse(fever==1,"fever",ifelse(diarrhea==1,"diarrhea",ifelse(dispnea==1,"dispnea",ifelse(neurologic==1,"neurologic",ifelse(otherSymp==1,"other symtomes", NA))))))

The problem is that the output results in only 4 categories, not 6.

I know this is a basic question, sorry and thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

臻嫒无言 2025-02-16 01:13:08

如评论中所述, case_when 是一个有用的选择到许多嵌套的 ifelse 调用:

library(dplyr)

# Create sample dataset
df <- data.frame(cough = c(1, 0, 0, 0, 0, 0, 0, 1),
                 fever = c(0, 1, 0, 0, 0, 0, 0, 1),
                 diarrhea = c(0, 0, 1, 0, 0, 0, 0, 1),
                 dyspnea = c(0, 0, 0, 1, 0, 0, 0, 1),
                 neurologic = c(0, 0, 0, 0, 1, 0, 0, 0),
                 otherSymp = c(0, 0, 0, 0, 0, 1, 0, 0))

# Create categorical variable
df |> mutate(symptom = case_when(cough == 1 ~ "cough",
                                 fever == 1 ~ "fever",
                                 diarrhea == 1 ~ "diarrhea",
                                 dyspnea == 1 ~ "dyspnea",
                                 neurologic == 1 ~ "neurologic",
                                 otherSymp == 1 ~ "other",
                                 TRUE ~ "none"))

输出

#>   cough fever diarrhea dyspnea neurologic otherSymp    symptom
#> 1     1     0        0       0          0         0      cough
#> 2     0     1        0       0          0         0      fever
#> 3     0     0        1       0          0         0   diarrhea
#> 4     0     0        0       1          0         0    dyspnea
#> 5     0     0        0       0          1         0 neurologic
#> 6     0     0        0       0          0         1      other
#> 7     0     0        0       0          0         0       none
#> 8     1     1        1       1          0         0      cough

在2022-07-11创建的(v2.0.1)

但是,这是一个非常基本的解决方案,假设您的症状不能超过一个(不合理) - 如果您有一个以上的症状,症状< /代码>只会提及第一个。

As mentioned in the comments, case_when is a helpful alternative to many nested ifelse calls:

library(dplyr)

# Create sample dataset
df <- data.frame(cough = c(1, 0, 0, 0, 0, 0, 0, 1),
                 fever = c(0, 1, 0, 0, 0, 0, 0, 1),
                 diarrhea = c(0, 0, 1, 0, 0, 0, 0, 1),
                 dyspnea = c(0, 0, 0, 1, 0, 0, 0, 1),
                 neurologic = c(0, 0, 0, 0, 1, 0, 0, 0),
                 otherSymp = c(0, 0, 0, 0, 0, 1, 0, 0))

# Create categorical variable
df |> mutate(symptom = case_when(cough == 1 ~ "cough",
                                 fever == 1 ~ "fever",
                                 diarrhea == 1 ~ "diarrhea",
                                 dyspnea == 1 ~ "dyspnea",
                                 neurologic == 1 ~ "neurologic",
                                 otherSymp == 1 ~ "other",
                                 TRUE ~ "none"))

Output

#>   cough fever diarrhea dyspnea neurologic otherSymp    symptom
#> 1     1     0        0       0          0         0      cough
#> 2     0     1        0       0          0         0      fever
#> 3     0     0        1       0          0         0   diarrhea
#> 4     0     0        0       1          0         0    dyspnea
#> 5     0     0        0       0          1         0 neurologic
#> 6     0     0        0       0          0         1      other
#> 7     0     0        0       0          0         0       none
#> 8     1     1        1       1          0         0      cough

Created on 2022-07-11 by the reprex package (v2.0.1)

However, this is a very basic solution, which assumes that you can't have more than one symptom (unreasonable) - if you have more than one, symptom will only mention the first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文