R 菜鸟入门篇 第09篇 字符串

R 用单引号或双引号来表示字符串。其实前面我们已经接触过了:

pm <- "c:\\R\\data\\dapengde_DummyR_PM25.csv"
pm
## [1] "c:\\R\\data\\dapengde_DummyR_PM25.csv"
class(pm)
## [1] "character"

R 可以很灵活地把多个字符串 合并

paste("A", "B")
## [1] "A B"
paste("A", "B", sep = "_")
## [1] "A_B"
paste(c("A", "B"), 3)
## [1] "A 3" "B 3"
paste(c("A", "B"), 3:4)
## [1] "A 3" "B 4"
paste(c("A", "B"), 3:4, sep = "_")
## [1] "A_3" "B_4"
paste(c("A", "B"), 3:4, sep = "_", collapse = "-")
## [1] "A_3-B_4"

x <- 34
threshold <- 99
if (x < threshold) print(paste("x is less than", threshold))
## [1] "x is less than 99"

path <- "c:\\R\\data"
file <- "dapengde_DummyR_PM25.csv"
paste(path, file, sep = "\\")
## [1] "c:\\R\\data\\dapengde_DummyR_PM25.csv"

cat("可以换行", "\n", "也可以", "\t", "tab") #连接字符,可加入换行和tab。
## 可以换行 
##  也可以    tab

分割和截取

nchar(x)
## [1] 2
strsplit("dapengde.com", "e")
## [[1]]
## [1] "dap"  "ngd"  ".com"
substr("dapengde.com", 1, 6)
## [1] "dapeng"
substring("dapengde.com", 1:10, 1:10)  # 这条指令换做substr试试有何不同。
##  [1] "d" "a" "p" "e" "n" "g" "d" "e" "." "c"

查找和替换

grep("e", c("dapeng", "de", ".com"))
## [1] 1 2
gsub("d", "D", c("dapengde", "de", ".com"))
## [1] "DapengDe" "De"       ".com"
sub("d", "D", c("dapengde", "de", ".com"))
## [1] "Dapengde" "De"       ".com"
chartr("de", "cn", "dapengde.com")
## [1] "capnngcn.com"

大小写转换:

tolower("DAPENGDE")
## [1] "dapengde"
toupper("com")
## [1] "COM"

将数字转换成字符串。

as.character(2)
## [1] "2"
formatC(2, width = 2, flag = "0")
## [1] "02"

将上面这些命令组合起来,R 对字符串的操作无所不能。

有用的信息:

字符串合并 paste(), cat()
字符串分割和截取 strsplit(), substr(), substring()
字符串查找和替换 grep(), gsub(), sub(), chartr()
字符串大小写转换 tolower(), toupper()
将数字转换成字符串 as.character(), formatC()

( 连载中,待续 )

R 菜鸟入门篇 第09篇 字符串》上有8条评论

  1. 卡卡

    cat(“可以换行”, “\n”, “也可以”, “\t”, “tab”)符,可加入换行和tab。
    —–
    少了注释符号吧

    回复
    1. dapeng 文章作者

      是的。原文是Rmd写的,转成html就莫名其妙丢了几个字符。已修正。谢谢指正。

      回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax