本文首发在CSDN博客,地址:http://blog.csdn.net/xxzhangx/article/details/53464140
欢迎关注我的博客!
这期介绍数组、字符串的翻转,元素间翻转,元素内翻转,采用rev、str_rev函数,并给出几个例子。
rev
元素间翻转
> x <- c(1:5, 5:3)
> x
[1] 1 2 3 4 5 5 4 3
> rev(x)
[1] 3 4 5 5 4 3 2 1
> x <- c("ABC", "DEF", "GHIJ")
> rev(x)
[1] "GHIJ" "DEF" "ABC"
str_rev
元素内翻转
> install.packages("Kmisc")
> library(Kmisc)
> x <- c("ABC", "DEF", "GHIJ")
> x
[1] "ABC" "DEF" "GHIJ"
> str_rev(x)
[1] "CBA" "FED" "JIHG"
> x <- c(1:5, 5:3)
> x
[1] 1 2 3 4 5 5 4 3
> str_rev(x)
[1] "1" "2" "3" "4" "5" "5" "4" "3"
> x <- "I am here."
> x
[1] "I am here."
> str_rev(x)
[1] ".ereh ma I"
> rev(x)
[1] "I am here."