a <- '使用单引号'
print(a)
[1] "使用单引号"
b <- "使用双引号"
print(b)
[1] "使用双引号"
c <- "双引号字符串中可以包含单引号(') "
print(c)
[1] "双引号字符串中可以包含单引号(') "
d <- '单引号字符串中可以包含双引号(") '
print(d)
[1] "单引号字符串中可以包含双引号(\") "
paste(..., sep = " ", collapse = NULL)
a <- "Google"
b <- 'Runoob'
c <- "Taobao"
print(paste(a,b,c))
[1] "Google Runoob Taobao"
print(paste(a,b,c, sep = "-"))
[1] "Google-Runoob-Taobao"
print(paste(letters[1:6],1:6, sep = "", collapse = "="))
[1] "a1=b2=c3=d4=e5=f6"
paste(letters[1:6],1:6, collapse = ".")
format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))
显示 9 位,最后一位四舍五入:
result <- format(23.123456789, digits = 9)
print(result)
[1] "23.1234568"
使用科学计数法显示:
result <- format(c(6, 13.14521), scientific = TRUE)
print(result)
[1] "6.000000e+00" "1.314521e+01"
小数点右边最小显示 5 位,没有的以 0 补充:
result <- format(23.47, nsmall = 5)
print(result)
[1] "23.47000"
将数字转为字符串:
result <- format(6)
print(result)
[1] "6"
宽度为 6 位,不够的在开头添加空格:
result <- format(13.7, width = 6)
print(result)
[1] " 13.7"
左对齐字符串:
result <- format("Runoob", width = 9, justify = "l")
print(result)
[1] "Runoob "
居中显示:
result <- format("Runoob", width = 10, justify = "c")
print(result)
[1] " Runoob "
nchar(x)
result <- nchar("Google Runoob Taobao")
print(result)
[1] 20
toupper(x)
tolower(x)
转大写:
result <- toupper("Runoob")
print(result)
[1] "RUNOOB"
转小写:
result <- tolower("Runoob")
print(result)
[1] "runoob"
substring(x,first,last)
从第 2 位截取到第 5 位:
result <- substring("Runoob", 2, 5)
print(result)
[1] "unoo"