我有一个带有*和-的数据框,我想将*转换为1,将-转换为0,但我不知道该怎么做
这是我的数据框
> head(Patient_036)
Driver SNV_Organoid_036 INDEL_Organoid_036 Deletion_Organoid_036
ABCB1 * - * -
ACVR1B * * * -
ACVR2A * * - -
APC * - * -
ARID1A * - * -
ARID1B * * - -
请帮忙吗?
答案 0 :(得分:0)
Patient_036[Patient_036 == "*"] <- 1
Patient_036[Patient_036 == "-"] <- 0
数据:
Patient_036 <- structure(list(Driver = c("1", "1", "1", "1", "1", "1"), SNV_Organoid_036 = c("0",
"1", "1", "0", "0", "1"), INDEL_Organoid_036 = c("1", "1", "0",
"1", "1", "0"), Deletion_Organoid_036 = c("0", "0", "0", "0",
"0", "0")), row.names = c("ABCB1", "ACVR1B", "ACVR2A", "APC",
"ARID1A", "ARID1B"), class = "data.frame")
如果您的数据框包含factors
,则可能首先需要df[] <- lapply(df, as.character)
。如果您在读取数据时添加stringsAsFactors = FALSE
或类似的内容,则不需要这样做。