r - How to delete characters in a string according to a second string? -
consider these 2 strings:
string1 <- "gctccc...ctccatgaagta...cttcacatccgtgt.ccggcctggccgcggagagccc" string_reference <- "gctccc...ctccatgaagtatttcttcacatccgtgt.ccggcctggccgcggagagccc" how remove dots in "string1", dots in same position in "string_reference"?
expected output:
string1 = "gctcccctccatgaagta...cttcacatccgtgtccggcctggccgcggagagccc"
similar robert's, "vectorized" version:
s1 <- unlist(strsplit(string1, "")) s2 <- unlist(strsplit(string_reference, "")) paste0(filter(negate(is.na), ifelse(s1 == s2 & s1 == ".", na, s1)), collapse="") # [1] "gctcccctccatgaagta...cttcacatccgtgtccggcctggccgcggagagccc" i quote "vectorized" because vectorization happening on characters of string vectors. assumes there 1 element in string vectors. if had multiple elements in string vectors have loop through results of strsplit.
Comments
Post a Comment