位置:首頁 > 大數據教學 > 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)

當上述代碼運行時,我們得到以下的輸出:

...: unexpected INCOMPLETE_STRING

.... unexpected symbol 
1: f <- 'Single quote ' inside

unexpected symbol
1: g <- "Double quotes " inside

字符串操作

連接字符串 - 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  "

統計字符串的字符數 - ncahr()函數

函數計算字符數量,包括在一個字符串的空格的個數。

語法

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"