在R中的單引號或雙引號中寫入的任何值都將被視為字串。在R內部將每個字串存儲在雙引號內,即使您使用單引號創建它們。
適用於字串構造的規則
- 字串開頭和結尾的引號應為雙引號或雙引號,他們不能混合。
- 雙引號可以插入到以單引號開始和結尾的字串中。
- 單引號可以插入到以雙引號開始和結尾的字串中。
- 雙引號不能插入到以雙引號開始和結尾的字串中。
- 單引號無法插入到以單引號開始和結尾的字串中。
有效字串的示例
以下示例闡明了在R中創建字串的規則。
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)
當上面的代碼運行時,我們得到以下輸出 -
[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"
無效字串的示例
e <- 'Mixed quotes"
print(e)
f <- 'Single quote ' inside single quote'
print(f)
g <- "Double quotes " inside double quotes"
print(g)
當我們運行腳本時,它不能正常執行並給出以下結果。
Error: unexpected symbol in:
"print(e)
f <- 'Single"
Execution halted
字串操作
連接字串 - paste()函數
R中的許多字串使用paste()
函數進行組合,可以將任意數量的參數組合在一起。
語法
粘貼函數的基本語法是 -
paste(..., sep = " ", collapse = NULL)
以下是使用的參數的描述 -
- … - 表示要組合的任何數量的參數。
- sep - 表示參數之間的任何分隔符號,這是一個可選項。
- collapse - 用於消除兩個字串之間的空格,但不是一個字串的兩個單詞之間的空格。
例子
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 = ""))
當我們執行上述代碼時,會產生以下結果 -
[1] "Hello How are you? "
[1] "Hello-How-are you? "
[1] "HelloHoware you? "
格式化數字和字串 - format()函數
可以使用format()
函數將數字和字串格式化為特定樣式。
語法
format()
函數的基本語法是 -
format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))
以下是使用的參數的描述 -
- x - 是輸入的向量。
- digits - 是顯示的總數。
- nsmall - 小數點右側的最小位數。
- scientific - 設置為
TRUE
,以顯示科學符號。 - width - 表示開始填充空白時要顯示的最小寬度。
- justify - 是將字串顯示為左,右或中心。
例子
# 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)
當我們執行上述代碼時,會產生以下結果 -
[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] " 13.7"
[1] "Hello "
[1] " Hello "
計數字串中的字元數 - nchar()函數
此函數計算字串中包含空格的字元數。
nchar()
函數的基本語法是 -
nchar(x)
以下是使用的參數的描述 -
- x - 是輸入的向量。
示例
result <- nchar("Count the number of characters")
print(result)
當我們執行上述代碼時,會產生以下結果 -
[1] 30
更改大小寫 - toupper()&tolower()函數
這些函數可以改變字串的字元。
語法
toupper()&tolower()
函數的基本語法是 -
toupper(x)
tolower(x)
以下是使用的參數的描述 -
- x - 是輸入的向量。
例子
# Changing to Upper case.
result <- toupper("Changing To Upper")
print(result)
# Changing to lower case.
result <- tolower("Changing To Lower")
print(result)
當我們執行上述代碼時,會產生以下結果 -
[1] "CHANGING TO UPPER"
[1] "changing to lower"
提取字串的substring()函數
此函數提取String的部分。
語法
substring()
函數的基本語法是 -
substring(x,first,last)
以下是使用的參數的描述 -
- x - 是字元輸入向量。
- first - 是要提取的第一個字元的位置。
- last - 是要提取的最後一個字元的位置。
例子
# Extract characters from 5th to 7th position.
result <- substring("Extract", 5, 7)
print(result)
當我們執行上述代碼時,會產生以下結果 -
[1] "act"