[Solved] Output left or right part of a matched string [closed]


Here are a couple ways to only keep the text that comes before your pattern, if it exists

a <- "GCUGUGGAGAUAACUGCGC"
b <- "CUGGCUGAGGUAGUAGUUUGUGCUGUUGGUCGGGUUGUGACAUUGCCCGCUGUGGAGAUAACUGCGCAAGC"

strsplit(b, a)[[1]][1]
sub(paste0(a, ".*$"), "", b)

Now, you just need to read the files into R and loop over each pattern. I’m not exactly sure what you are looking for, but here is an idea

# read data into 2 variables, a and b
# you could use readLines() to do read from disk
a <- readLines(textConnection("ACUGUACAGGCCACUGCCUUGC
CUGCGCAAGCUACUGCCUUGCU
UGGAAUGUAAAGAAGUAUGUAU
CGAAUCAUUAUUUGCUGCUCUA
AUCACAUUGCCAGGGAUUACC
UUCACAGUGGCUAAGUUCUGC"))

b <- readLines(textConnection("CCAGGCUGAGGUAGUAGUUUGUACAGUUUGAGGGUCUAUGAUACCACCCGGUACAGGAGAUAACUGUACAGGCCACUGCCUUGCCAGG
CUGGCUGAGGUAGUAGUUUGUGCUGUUGGUCGGGUUGUGACAUUGCCCGCUGUGGAGAUAACUGCGCAAGCUACUGCCUUGCUAG
GCUUGGGACACAUACUUCUUUAUAUGCCCAUAUGAACCUGCUAAGCUAUGGAAUGUAAAGAAGUAUGUAUUUCAGGC
CUGUAGCAGCACAUCAUGGUUUACAUACUACAGUCAAGAUGCGAAUCAUUAUUUGCUGCUCUAG
GGCUGCUUGGGUUCCUGGCAUGCUGAUUUGUGACUUGAGAUUAAAAUCACAUUGCCAGGGAUUACCACGCAACC"))

Now, loop over each value from the first file

lapply(a, function(x) sapply(strsplit(b, x), "[", 1))

2

solved Output left or right part of a matched string [closed]