码迷,mamicode.com
首页 > 其他好文 > 详细

R6字符串函数

时间:2017-11-30 17:17:47      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:取字符串   填充   char   字符   fan   连接   class   ber   tab   

1连接字符串 - paste()函数

paste(..., sep = " ", collapse = NULL)

...表示要组合的任意数量的自变量。
sep表示参数之间的任何分隔符。 它是可选的。
collapse用于消除两个字符串之间的空格。

a <- "Hello"

b <- ‘How‘

c <- "are you? "

print(paste(a,b,c, sep = "-"))

 "Hello-How-are you? "

2可以使用format()函数将数字和字符串格式化为特定样式

格式化函数的基本语法是 -

format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none")) 

以下是所使用的参数的描述

  • x是向量输入。
  • digits是显示的总位数。result <- format(23.123456789, digits = 9)  //"23.1234568"

 

  • nsmall是小数点右边的最小位数。result <- format(23.47, nsmall = 5)//"23.47000"
  • 科学设置为TRUE以显示科学记数法。result <- format(c(6, 13.14521), scientific = TRUE) //"6.000000e+00" "1.314521e+01"
  • width指示通过在开始处填充空白来显示的最小宽度。result <- format(13.7, width = 6)//"  13.7"
  • justify是字符串向左,右或中心的显示。result <- format("Hello", width = 8, justify = "l") "Hello   "

result <- format("Hello", width = 8, justify = "c")" Hello  "

 

3计算字符串中的字符数 - nchar()函数//此函数计算字符串中包含空格的字符数

result <- nchar("Count the number of characters") //30

> x<-c("aa","aaaa")

> nchar(x)

[1] 2 4

4这些函数改变字符串的字符的大小写toupper()tolower()函数

> toupper("Asds")

[1] "ASDS"

> tolower("Asds")

[1] "asds"
5
提取字符串的一部分 - substring()函数substring(x,first,last)

首先是要提取的第一个字符的位置。

last是要提取的最后一个字符的位置

result <- substring("Extract", 5, 7)  //"act"


 

 

R6字符串函数

标签:取字符串   填充   char   字符   fan   连接   class   ber   tab   

原文地址:http://www.cnblogs.com/keiweila/p/7929669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!