Reputation: 33
I have a data.frame which I have read from a csv file. This data have 440 rows and 900 columns. I need to focus on column one and manipulate the string to leave only 2 characters remaining in the column for each row. This data also varies in size. examples:
4 ADCY7_S3
5 AIMP2_S5
6 ALKBH7_S5
7 ALOX5AP_S3
The information I need is S1,S2,S3,S4 or S5.
Upvotes: 1
Views: 80
Reputation: 887068
We can use substring
from base R
if the number of characters to be extracted is 2 from the last position of each string.
substring(v1, nchar(v1)-1)
#[1] "S3" "S5" "S5" "S3"
Or with sub
to match characters until the _
and replace it with blank (""
)
sub(".*_", "", v1)
v1 <- c("ADCY7_S3", "AIMP2_S5", "ALKBH7_S5", "ALOX5AP_S3")
Upvotes: 2