简体   繁体   中英

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

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. This won't be necessary if you add stringsAsFactors = FALSE or something equivalent when you read in the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM