溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

R語言中字符串有哪些知識點

發(fā)布時間:2021-03-29 09:26:42 來源:億速云 閱讀:228 作者:小新 欄目:開發(fā)技術

這篇文章主要介紹了R語言中字符串有哪些知識點,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

在R語言中的單引號或雙引號對中寫入的任何值都被視為字符串。 R語言存儲的每個字符串都在雙引號內,即使是使用單引號創(chuàng)建的依舊如此。

在字符串構造中應用的規(guī)則

  • 在字符串的開頭和結尾的引號應該是兩個雙引號或兩個單引號。它們不能被混合。

  • 雙引號可以插入到以單引號開頭和結尾的字符串中。

  • 單引號可以插入以雙引號開頭和結尾的字符串。

  • 雙引號不能插入以雙引號開頭和結尾的字符串。

  • 單引號不能插入以單引號開頭和結尾的字符串。

有效字符串的示例

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

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() 函數(shù)

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

語法

對于粘貼功能的基本語法是

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

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

  • ... 表示要組合的任意數(shù)量的自變量。

  • sep 表示參數(shù)之間的任何分隔符。它是可選的。

  • 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 = ""))

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

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

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

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

語法

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

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

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

  • x 是向量輸入。

  • digits 是顯示的總位數(shù)。

  • nsmall 是小數(shù)點右邊的最小位數(shù)。

  • 科學設置為 TRUE 以顯示科學記數(shù)法。

  • 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)

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

[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] " 13.7"
[1] "Hello  "
[1] " Hello "

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

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

語法

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

nchar(x)

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

x 是向量輸入。

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

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

[1] 30

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

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

語法

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

toupper(x)
tolower(x)

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

x是向量輸入。

# Changing to Upper case.
result <- toupper("Changing To Upper")
print(result)

# Changing to lower case.
result <- tolower("Changing To Lower")
print(result)

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

提取

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

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

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

語法

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

substring(x,first,last)

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

  • x 是字符向量輸入。

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

  • last 是要提取的最后一個字符的位置。

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

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

[1] "act"

感謝你能夠認真閱讀完這篇文章,希望小編分享的“R語言中字符串有哪些知識點”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI