如何在 R 中将一个类别从一列转移到另一列?
how to get one category from a column into another column, in R?
我也在尝试将一个类别从一列添加到另一列,但将其保留为一个类别。这是一个数据样本:
structure(list(id = c("117dbbbf15", "117dbbbf15", "117dbbbf15",
"117dbbbf15", "117dbbbf15", "117dbbbf15", "117dbbbf15", "117dbbbf15",
"117dbbbf15", "117dbbbf15"), covid_tested = c("positive", "positive",
"positive", "positive", "positive", "positive", "positive", "positive",
"positive", "positive"), chills = c("No", "No", "No", "No", "No",
"No", "No", "No", "No", "No"), cough = c("Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"), diarrhoea = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), fatigue = c("Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"
), headache = c("No", "No", "No", "No", "No", "No", "No", "No",
"No", "No"), loss_smell_taste = c("No", "No", "No", "No", "No",
"No", "No", "No", "No", "No"), muscle_ache = c("No", "No", "No",
"No", "No", "No", "No", "No", "No", "No"), nasal_congestion = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), nausea_vomiting = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), shortness_breath = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), sore_throat = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), sputum = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), temperature = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), loss_appetite = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), chest_pain = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), comorbidities = c("itchy_eyes",
"joint_pain", "asthma", "diabetes_type_one", "diabetes_type_two",
"obesity", "hypertension", "heart_disease", "lung_condition",
"liver_disease"), bolean = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L), .Label = c("healthy", "Yes"), class = "factor")), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
如您所见,布尔列有一个健康类别,我想将其作为合并症的类别。有没有办法在 R 中执行此操作,最好使用 tidyverse?
这是否解决了您的问题?
library(dplyr)
df %>%
mutate(
b = as.character(bolean),
unhealthy = b != "healthy",
comorbidities = replace(b, unhealthy, comorbidities[unhealthy]),
b = NULL, unhealthy = NULL
)
为此,我会在 dplyr 中使用 case_when()。我创建了一个名为 comorbidities2 的新列来显示差异,但它可以被覆盖(用合并症替换 comorbidites2):
df %>%
mutate(comorbidities2 = case_when(bolean == "healthy" ~ "healthy",
TRUE ~ comorbidities))
我也在尝试将一个类别从一列添加到另一列,但将其保留为一个类别。这是一个数据样本:
structure(list(id = c("117dbbbf15", "117dbbbf15", "117dbbbf15",
"117dbbbf15", "117dbbbf15", "117dbbbf15", "117dbbbf15", "117dbbbf15",
"117dbbbf15", "117dbbbf15"), covid_tested = c("positive", "positive",
"positive", "positive", "positive", "positive", "positive", "positive",
"positive", "positive"), chills = c("No", "No", "No", "No", "No",
"No", "No", "No", "No", "No"), cough = c("Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"), diarrhoea = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), fatigue = c("Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"
), headache = c("No", "No", "No", "No", "No", "No", "No", "No",
"No", "No"), loss_smell_taste = c("No", "No", "No", "No", "No",
"No", "No", "No", "No", "No"), muscle_ache = c("No", "No", "No",
"No", "No", "No", "No", "No", "No", "No"), nasal_congestion = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), nausea_vomiting = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), shortness_breath = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), sore_throat = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), sputum = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), temperature = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), loss_appetite = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), chest_pain = c("No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), comorbidities = c("itchy_eyes",
"joint_pain", "asthma", "diabetes_type_one", "diabetes_type_two",
"obesity", "hypertension", "heart_disease", "lung_condition",
"liver_disease"), bolean = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L), .Label = c("healthy", "Yes"), class = "factor")), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
如您所见,布尔列有一个健康类别,我想将其作为合并症的类别。有没有办法在 R 中执行此操作,最好使用 tidyverse?
这是否解决了您的问题?
library(dplyr)
df %>%
mutate(
b = as.character(bolean),
unhealthy = b != "healthy",
comorbidities = replace(b, unhealthy, comorbidities[unhealthy]),
b = NULL, unhealthy = NULL
)
为此,我会在 dplyr 中使用 case_when()。我创建了一个名为 comorbidities2 的新列来显示差异,但它可以被覆盖(用合并症替换 comorbidites2):
df %>%
mutate(comorbidities2 = case_when(bolean == "healthy" ~ "healthy",
TRUE ~ comorbidities))