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