[R] Substring and extract a certain number of characters

arun smartpink111 at yahoo.com
Thu Nov 14 17:02:38 CET 2013


Also,
library(stringr)
str_sub(s2,-5,-1)

str_extract(s1,"[[:alpha:]]+")

A.K.





On Thursday, November 14, 2013 10:50 AM, arun <smartpink111 at yahoo.com> wrote:
Try:
s1 <- "hello.world"
s2 <- c("GLEm0045", "GLEn0042", "GLEz0048")

 substr(s1,1,5)
#or
gsub("\\..*","",s1)

n <- 5
substr(s2,nchar(s2)-n +1,nchar(s2))


A.K.



If I have a column that has "hello.world"  how   could I extract hello? 
 In addition in a column how can I extract the last 5 characters?    For example from character  G1Em0017  I wish to extract m0017 .



More information about the R-help mailing list