How to subset String object in R

45 Views Asked by At

I have an AAString () object like below, how can I subset for multiple positions?

df <- AAString("HAKTKIDLTBI")
df

11-letter AAString object
seq: HAKTKIDLTBI

I want to subset for :

substring(df,c(1,3,4,10))

Output:

seq: HKTB
2

There are 2 best solutions below

0
Stefano Barbi On BEST ANSWER

You can use standard indices

df <- AAString("HAKTKIDLTBI")
df[c(1,3,4,10)]
##  4-letter AAString object
##  seq: HKTB
1
ThomasIsCoding On

I am not sure if the approach applies to AAString object, but you can have a try

> s <- "HAKTKIDLTBI"

> idx <- c(1, 3, 4, 10)

> paste0(substring(s, idx, idx), collapse = "")
[1] "HKTB"