[英]How I convert characters to binary values in R
I have a dataframe with * and - I want to convert * to 1 and - to 0 but I don't know what to do 我有一个带*和 - 的数据框我希望将*转换为1和 - 到0但我不知道该怎么做
This is my dataframe 这是我的数据框架
> head(Patient_036)
Driver SNV_Organoid_036 INDEL_Organoid_036 Deletion_Organoid_036
ABCB1 * - * -
ACVR1B * * * -
ACVR2A * * - -
APC * - * -
ARID1A * - * -
ARID1B * * - -
Any help please? 有什么帮助吗?
Patient_036[Patient_036 == "*"] <- 1
Patient_036[Patient_036 == "-"] <- 0
DATA: 数据:
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")
In case your dataframe contains factors
, you may need df[] <- lapply(df, as.character)
at first. 如果您的数据框包含
factors
,您可能首先需要df[] <- lapply(df, as.character)
。 This won't be necessary if you add stringsAsFactors = FALSE
or something equivalent when you read in the data. 如果在读入数据时添加
stringsAsFactors = FALSE
或等效的内容,则stringsAsFactors = FALSE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.