Examples for 'base::substr'


Substrings of a Character Vector

Aliases: substr substring substr<- substring<-

Keywords: character

### ** Examples

substr("abcdef", 2, 4)
[1] "bcd"
substring("abcdef", 1:6, 1:6)
[1] "a" "b" "c" "d" "e" "f"
## strsplit() is more efficient ...

substr(rep("abcdef", 4), 1:4, 4:5)
[1] "abcd" "bcde" "cd"   "de"  
x <- c("asfef", "qwerty", "yuiop[", "b", "stuff.blah.yech")
substr(x, 2, 5)
[1] "sfef" "wert" "uiop" ""     "tuff"
substring(x, 2, 4:6)
[1] "sfe"   "wert"  "uiop[" ""      "tuff" 
X <- x
names(X) <- LETTERS[seq_along(x)]
comment(X) <- noquote("is a named vector")
str(aX <- attributes(X))
List of 2
 $ names  : chr [1:5] "A" "B" "C" "D" ...
 $ comment: 'noquote' chr "is a named vector"
substring(x, 2) <- c("..", "+++")
substring(X, 2) <- c("..", "+++")
X
                A                 B                 C                 D 
          "a..ef"          "q+++ty"          "y..op["               "b" 
                E 
"s..ff.blah.yech" 
stopifnot(x == X, identical(aX, attributes(X)), nzchar(comment(X)))

[Package base version 4.2.3 Index]