我有癌症数据,每个病人都做了1-4次测量.有些测量做了细胞学判断,有些做了病理学判断,有些两者都做了.
library(dplyr)
library(tibble)
data<-tribble(
~record_number, ~tool, ~cytology, ~pathology,
114, "forceps", "Indeterminate", NA,
114, "needle", "Non-Malignant", "Malignant",
114, "lavage", NA, "Indeterminate",
115, "forceps", NA, "Non-Malignant",
115, "needle", NA, "Malignant"
)
如果给定主题(RECORD_NUMBER)的either列(细胞学、病理学)中的任何样本(行)出现"恶性",我想创建一个恶性变量(0/1).
任何 idea 都很受欢迎!
desired<-tribble(
~record_number, ~tool, ~cytology, ~pathology, ~ Malignant,
114, "forceps", "Indeterminate", NA, 1,
114, "needle", "Non-Malignant", "Malignant", 1,
114, "lavage", NA, "Indeterminate", 1,
115, "forceps", NA, "Non-Malignant", 1,
115, "needle", NA, "Malignant", 1,
)
我想它将以GROUP_BY(RECORD_NUMBER)开始……但是然后呢?
desired<-data %>%
group_by(record_number) %>%
...?