简体   繁体   中英

Sum of columns with partial string match R

I want to add two columns based on partial matches of their string value

a <- c("ChrM","ChrM","ChrM","ChrM","ChrM")
b <- c(5,6,7,10,11)
c <- c(0,0,3,0,1)
d <- c(2,1,0,1,0)

dfa <- data.table(a, b, c, d) 
colnames(dfa) <- c("ID","pos","cr H-MN-8A","cr H-MN-8B")
#I don't know how to make a column name with a string value (with " " and "_")
  ID     pos        cr H-MN-8A cr H-MN-8B
1 ChrM   5          0          2
2 ChrM   6          0          1
3 ChrM   7          3          0
4 ChrM  10          0          1
5 ChrM  11          1          0

I want to be able to add the last 2 columns "cr H-MN-8A","cr H-MN-8B" based on their partial string match "cr H-MN-8" and then write the result into a column while also comparing it to a value, so the return is true or false. I had something akin to

dfa <- lapply(dfa, function(x) x[, newval := as.numeric(col1+col2>=1)])

I have more than two string matches withing titles (they're all paired up on the same dataframe). Any ideas? Thanks!

(Don't know if it's relevant, but I'm using data.table

We can try the following data.table approach

dfa[,`H-MN-8` := as.numeric(rowSums(.SD) > 1), .SDcols = grep("cr H-MN-8", names(dfa))]

dfa
#     ID pos cr H-MN-8A cr H-MN-8B H-MN-8
#1: ChrM   5          0          2      1
#2: ChrM   6          0          1      0
#3: ChrM   7          3          0      1
#4: ChrM  10          0          1      0
#5: ChrM  11          1          0      0

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