一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - R語(yǔ)言 - R語(yǔ)言字符串知識(shí)點(diǎn)總結(jié)及實(shí)例分析

R語(yǔ)言字符串知識(shí)點(diǎn)總結(jié)及實(shí)例分析

2022-01-04 15:37w3cschool R語(yǔ)言

在本篇文章里小編給各位分享的是一篇關(guān)于R語(yǔ)言字符串知識(shí)點(diǎn)總結(jié)及實(shí)例分析,有興趣的朋友們可以學(xué)習(xí)下。

在R語(yǔ)言中的單引號(hào)或雙引號(hào)對(duì)中寫(xiě)入的任何值都被視為字符串。 R語(yǔ)言存儲(chǔ)的每個(gè)字符串都在雙引號(hào)內(nèi),即使是使用單引號(hào)創(chuàng)建的依舊如此。

在字符串構(gòu)造中應(yīng)用的規(guī)則

  • 在字符串的開(kāi)頭和結(jié)尾的引號(hào)應(yīng)該是兩個(gè)雙引號(hào)或兩個(gè)單引號(hào)。它們不能被混合。
  • 雙引號(hào)可以插入到以單引號(hào)開(kāi)頭和結(jié)尾的字符串中。
  • 單引號(hào)可以插入以雙引號(hào)開(kāi)頭和結(jié)尾的字符串。
  • 雙引號(hào)不能插入以雙引號(hào)開(kāi)頭和結(jié)尾的字符串。
  • 單引號(hào)不能插入以單引號(hào)開(kāi)頭和結(jié)尾的字符串。

有效字符串的示例

以下示例闡明了在 R 語(yǔ)言中創(chuàng)建字符串的規(guī)則。

?
1
2
3
4
5
6
7
8
9
10
11
a <- 'Start and end with single quote'
print(a)
 
b <- "Start and end with double quotes"
print(b)
 
c <- "single quote ' in between double quotes"
print(c)
 
d <- 'Double quotes " in between single quote'
print(d)

當(dāng)運(yùn)行上面的代碼,我們得到以下輸出

?
1
2
3
4
[1] "Start and end with single quote"
[1] "Start and end with double quotes"
[1] "single quote ' in between double quote"
[1] "Double quote " in between single quote"

無(wú)效字符串的示例

?
1
2
3
4
5
6
7
8
e <- 'Mixed quotes"
print(e)
 
f <- 'Single quote ' inside single quote'
print(f)
 
g <- "Double quotes " inside double quotes"
print(g)

當(dāng)我們運(yùn)行腳本失敗給下面的結(jié)果。

?
1
2
3
4
5
6
7
...: unexpected INCOMPLETE_STRING
 
.... unexpected symbol
1: f <- 'Single quote ' inside
 
unexpected symbol
1: g <- "Double quotes " inside

字符串操作

連接字符串 - paste() 函數(shù)

R語(yǔ)言中的許多字符串使用 paste() 函數(shù)組合。 它可以采取任何數(shù)量的參數(shù)組合在一起。

語(yǔ)法

對(duì)于粘貼功能的基本語(yǔ)法是

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

以下是所使用的參數(shù)的說(shuō)明 -

  • ... 表示要組合的任意數(shù)量的自變量。
  • sep 表示參數(shù)之間的任何分隔符。它是可選的。
  • collapse 用于消除兩個(gè)字符串之間的空格。 但不是一個(gè)字符串的兩個(gè)字內(nèi)的空間。

?
1
2
3
4
5
6
7
8
9
a <- "Hello"
b <- 'How'
c <- "are you? "
 
print(paste(a,b,c))
 
print(paste(a,b,c, sep = "-"))
 
print(paste(a,b,c, sep = "", collapse = ""))

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

?
1
2
3
[1] "Hello How are you? "
[1] "Hello-How-are you? "
[1] "HelloHoware you? "

格式化數(shù)字和字符串 - format() 函數(shù)

可以使用 format() 函數(shù)將數(shù)字和字符串格式化為特定樣式。

語(yǔ)法

格式化函數(shù)的基本語(yǔ)法是

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

以下是所使用的參數(shù)的描述 -

  • x 是向量輸入。
  • digits 是顯示的總位數(shù)。
  • nsmall 是小數(shù)點(diǎn)右邊的最小位數(shù)。
  • 科學(xué)設(shè)置為 TRUE 以顯示科學(xué)記數(shù)法。
  • width 指示通過(guò)在開(kāi)始處填充空白來(lái)顯示的最小寬度。
  • justify 是字符串向左,右或中心的顯示。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Total number of digits displayed. Last digit rounded off.
result <- format(23.123456789, digits = 9)
print(result)
 
# Display numbers in scientific notation.
result <- format(c(6, 13.14521), scientific = TRUE)
print(result)
 
# The minimum number of digits to the right of the decimal point.
result <- format(23.47, nsmall = 5)
print(result)
 
# Format treats everything as a string.
result <- format(6)
print(result)
 
# Numbers are padded with blank in the beginning for width.
result <- format(13.7, width = 6)
print(result)
 
# Left justify strings.
result <- format("Hello", width = 8, justify = "l")
print(result)
 
# Justfy string with center.
result <- format("Hello", width = 8, justify = "c")
print(result)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -

?
1
2
3
4
5
6
7
[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] " 13.7"
[1] "Hello  "
[1] " Hello "

計(jì)算字符串中的字符數(shù) - nchar() 函數(shù)

此函數(shù)計(jì)算字符串中包含空格的字符數(shù)。

語(yǔ)法

nchar() 函數(shù)的基本語(yǔ)法是

?
1
nchar(x)

以下是所使用的參數(shù)的描述 -

x 是向量輸入。

?
1
2
result <- nchar("Count the number of characters")
print(result)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

?
1
[1] 30

更改case - toupper()和tolower()函數(shù)

這些函數(shù)改變字符串的字符的大小寫(xiě)。

語(yǔ)法

toupper()和tolower()函數(shù)的基本語(yǔ)法是

?
1
2
toupper(x)
tolower(x)

以下是所使用的參數(shù)的描述 -

x是向量輸入。

?
1
2
3
4
5
6
7
# Changing to Upper case.
result <- toupper("Changing To Upper")
print(result)
 
# Changing to lower case.
result <- tolower("Changing To Lower")
print(result)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

提取

?
1
2
[1] "CHANGING TO UPPER"
[1] "changing to lower"

字符串的一部分 - substring()函數(shù)

此函數(shù)提取字符串的部分。

語(yǔ)法

substring() 函數(shù)的基本語(yǔ)法是

?
1
substring(x,first,last)

以下是所使用的參數(shù)的描述 -

  • x 是字符向量輸入。
  • 首先是要提取的第一個(gè)字符的位置。
  • last 是要提取的最后一個(gè)字符的位置。

?
1
2
3
# Extract characters from 5th to 7th position.
result <- substring("Extract", 5, 7)
print(result)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

?
1
[1] "act"

到此這篇關(guān)于R語(yǔ)言字符串知識(shí)點(diǎn)總結(jié)及實(shí)例分析的文章就介紹到這了,更多相關(guān)R語(yǔ)言字符串內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://www.w3cschool.cn/r/r_data_reshaping.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 校花被吃奶还摸下面 | 91视频综合网 | 校花在公车上被内射好舒 | 欧美特黄特色aaa大片免费看 | 青青草视频破解版 | 精品久久成人免费第三区 | 91九色国产porny | 毛片在线播放a | 99视频精品国在线视频艾草 | 婷婷在线观看香蕉五月天 | 2019韩国最新三级 | jk制服喷水 | 7777奇米四色 | 成人国产精品一区二区不卡 | 日韩伦理在线观看 | 午夜免费体验30分 | 欧美贵妇videos办公室 | 国产日韩欧美不卡www | 日本在线不卡免 | 精品综合久久久久久8888 | 欧美一区二区三区免费不卡 | 免费一区在线观看 | 欧美一级欧美三级 | 国产第一草草影院 | 国色天香论坛社区在线视频 | 天天久久影视色香综合网 | 天天综合网天天做天天受 | 娇妻与公陈峰姚瑶小说在线阅读 | 美女跪式抽搐gif动态图 | 亚洲 欧美 国产 在线 日韩 | 久草草在线视视频 | 黑人巨大初黑人解禁作品 | 久久草福利自拍视频在线观看 | 日日本老女人 | 久久一本综合 | 免费370理论片中文字幕 | 91蜜桃| 免费高清视频在线观看 | 五月天狠狠| 国产91免费在线 | 古装全套 毛片 |