在R中的單引號(hào)或雙引號(hào)中寫(xiě)入的任何值都將被視為字符串。在R內(nèi)部將每個(gè)字符串存儲(chǔ)在雙引號(hào)內(nèi),即使您使用單引號(hào)創(chuàng)建它們。
有效字符串的示例
以下示例闡明了在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)
當(dāng)上面的代碼運(yùn)行時(shí),我們得到以下輸出 -
[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ú)效字符串的示例
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)行腳本時(shí),它不能正常執(zhí)行并給出以下結(jié)果。
Error: unexpected symbol in:
"print(e)
f <- 'Single"
Execution halted
連接字符串 - paste()函數(shù)
R中的許多字符串使用paste()函數(shù)進(jìn)行組合,可以將任意數(shù)量的參數(shù)組合在一起。
語(yǔ)法
粘貼函數(shù)的基本語(yǔ)法是 -
paste(..., sep = " ", collapse = NULL)
以下是使用的參數(shù)的描述 -
例子
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í)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -
[1] "Hello How are you? "
[1] "Hello-How-are you? "
[1] "HelloHoware you? "
格式化數(shù)字和字符串 - format()函數(shù)
可以使用format()函數(shù)將數(shù)字和字符串格式化為特定樣式。
語(yǔ)法
format()函數(shù)的基本語(yǔ)法是 -
format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))
以下是使用的參數(shù)的描述 -
TRUE,以顯示科學(xué)符號(hào)。例子
# 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í)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -
[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] " 13.7"
[1] "Hello "
[1] " Hello "
計(jì)數(shù)字符串中的字符數(shù) - nchar()函數(shù)
此函數(shù)計(jì)算字符串中包含空格的字符數(shù)。
nchar()函數(shù)的基本語(yǔ)法是 -
nchar(x)
以下是使用的參數(shù)的描述 -
示例
result <- nchar("Count the number of characters")
print(result)
當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -
[1] 30
更改大小寫(xiě) - toupper()&tolower()函數(shù)
這些函數(shù)可以改變字符串的字符。
語(yǔ)法
toupper()&tolower()函數(shù)的基本語(yǔ)法是 -
toupper(x)
tolower(x)
以下是使用的參數(shù)的描述 -
例子
# 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í)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -
[1] "CHANGING TO UPPER"
[1] "changing to lower"
提取字符串的substring()函數(shù)
此函數(shù)提取String的部分。
語(yǔ)法
substring()函數(shù)的基本語(yǔ)法是 -substring(x,first,last)
以下是使用的參數(shù)的描述 -
例子
# Extract characters from 5th to 7th position.
result <- substring("Extract", 5, 7)
print(result)
當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -
[1] "act"