我如何将字符转换为 R 中的二进制值
How I convert characters to binary values in R
我有一个带有 * 和 - 我想将 * 转换为 1 和 - 为 0 但我不知道该怎么做的数据框
这是我的数据框
> head(Patient_036)
Driver SNV_Organoid_036 INDEL_Organoid_036 Deletion_Organoid_036
ABCB1 * - * -
ACVR1B * * * -
ACVR2A * * - -
APC * - * -
ARID1A * - * -
ARID1B * * - -
有什么帮助吗?
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
或等效内容,则没有必要这样做。
我有一个带有 * 和 - 我想将 * 转换为 1 和 - 为 0 但我不知道该怎么做的数据框
这是我的数据框
> head(Patient_036)
Driver SNV_Organoid_036 INDEL_Organoid_036 Deletion_Organoid_036
ABCB1 * - * -
ACVR1B * * * -
ACVR2A * * - -
APC * - * -
ARID1A * - * -
ARID1B * * - -
有什么帮助吗?
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
或等效内容,则没有必要这样做。