I'm trying to delete the first 7 characters of each string in a set of strings. I've been following akrun's wonderful answer here (how to add a character to a string in R). This is the command I've been using but it's returning an error:
backup_mouse_CTRL <- sub("(.{7})(.*)", "\\2", mouse_CTRL)
"Error in as.character.default(x) : no method for coercing this S4 class to a vector"
Is there an error in what I've written? Could you also help me a lot if someone could help me add these seven characters to every string in a list as well ("mm10---")? It would really help me for a later step in my analysis.
I've been trying to figure out how to paste a bit of my dataset here to allow for a reproducible example but my data is a Seurat object (useful for RNA-seq analysis in Biology) and the raw data is many gigabytes large. Because of that, I'm sorry I haven't been able to paste any data here.
I apologize if this question is basic - I have reviewed other similarly phrased questions and tried to integrate them into what I'm doing but I haven't had much luck unfortunately.
CodePudding user response:
You could use substring
, in combination with paste0
. If you want to replace each string individually, use mapply
, else sapply
:
mapply(\(x, y) paste0(y, substring(x, 8)), s, r)
# AAAABDBBADCDCD AABDBBCCAACDAC AADBDDCBABDBCC
# "ZYYZXXYACDABAA" "ZYXXXXXADDBADB" "YZXYXZYDCDADAD"
sapply(s, \(x, r) paste0(r, substring(x, 8)), r='XXXXXXX')
# AAAABDBBADCDCD AABDBBCCAACDAC AADBDDCBABDBCC
# "XXXXXXXBADCDCD" "XXXXXXXCAACDAC" "XXXXXXXBABDBCC"
Data:
s <- c("AAAABDBBADCDCD", "AABDBBCCAACDAC", "AADBDDCBABDBCC")
r <- c("YYYYXXY", "YZZXXYY", "YYYZYXY")