I have long vector of strings.
e.g. c('aecde1234e', 'ewert6e8e0')
I want to compare each chacter in each string to one specific lookup character, e.g. 'e'
.
The goal is to have a long vector of strings with the result
e.g. c('0100100001', '1010001010')
.
How can I write such a comparison in R ?
CodePudding user response:
You could do:
x <- c('aecde1234e', 'ewert6e8e0')
gsub("e", "1", gsub("[^e]", "0", x))
# [1] "0100100001" "1010001010"
CodePudding user response:
> string <- c('aecde1234e', 'ewert6e8e0')
> sapply(strsplit(string, ""), function(x) paste(as.numeric(x == "e"), collapse = "") )
[1] "0100100001" "1010001010"