
- R 教程
- R - 首頁
- R - 概述
- R - 環境設定
- R - 基本語法
- R - 資料型別
- R - 變數
- R - 運算子
- R - 決策
- R - 迴圈
- R - 函式
- R - 字串
- R - 向量
- R - 列表
- R - 矩陣
- R - 陣列
- R - 因子
- R - 資料框
- R - 包
- R - 資料重塑
R - 字串
在 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 函式的基本語法如下:
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() 函式
此函式提取字串的一部分。
語法
substring() 函式的基本語法如下:
substring(x,first,last)
以下是所用引數的描述:
**x** 是字元向量輸入。
**first** 是要提取的第一個字元的位置。
**last** 是要提取的最後一個字元的位置。
示例
# Extract characters from 5th to 7th position. result <- substring("Extract", 5, 7) print(result)
當我們執行以上程式碼時,它會產生以下結果:
[1] "act"