Saturday, February 9, 2008

R string manipulation

> x <- c("a", "b", "c"); > paste(x, 1:3, sep="..");
[1] "a..1" "b..2" "c..3"
> paste(x, collapse="..");
[1] "a..b..c"
> strsplit("a.b.c", ".", fixed = TRUE)
[[1]]
[1] "a" "b" "c"

> unlist(strsplit("a.b.c", ".", fixed = TRUE));
[1] "a" "b" "c"
> strtrim(c("abcdef", "abcdef", "abcdef"), c(1,5,10))
[1] "a" "abcde" "abcdef"
> substr("abcdef",2,4)
[1] "bcd"
> substring("abcdef",c(1, 3), c(2, 6))
[1] "ab" "cdef"
> strtrim(c("abcdef", "abcdef", "abcdef"), c(1,5,10))
[1] "a" "abcde" "abcdef"

#############################################
x <- c("a", "b", "c");
paste(x, 1:3, sep="..");
paste(x, collapse="..");
strsplit("a.b.c", ".", fixed = TRUE)
unlist(strsplit("a.b.c", ".", fixed = TRUE));
strtrim(c("abcdef", "abcdef", "abcdef"), c(1,5,10))
substr("abcdef",2,4)
substring("abcdef",c(1, 3), c(2, 6))
strtrim(c("abcdef", "abcdef", "abcdef"), c(1,5,10))

No comments: