R Strings
In R, strings are a data type that represents text data. They are composed of characters (character vectors) and can contain letters, numbers, symbols, spaces, and other characters.
R strings can use a pair of single quotes' 'or a pair of double quotes" "to represent them.
- Single-quoted strings can contain double quotes.
- Single-quoted strings cannot contain single quotes.
- Double-quoted strings can contain single quotes.
- Double-quoted strings cannot contain double quotes.
Creating Strings:You can use single quotes or double quotes to create strings.
The following example demonstrates the use of strings:
Example
print(a)
b <- "Using double quotes"
print(b)
c <- "Double-quoted strings can contain single quotes (') "
print(c)
d <- 'Single-quoted strings can contain double quotes (") '
print(d)
Executing the above code produces the following result:
[1] "使用单引号" [1] "使用双引号" [1] "双引号字符串中可以包含单引号(') " [1] "单引号字符串中可以包含双引号(\") "
String Operations
R provides a variety of functions and operators for manipulating strings, making it convenient to process and operate on text data.
Let's take a look at some built-in R functions for string operations.
paste() Function
The paste() function is used to concatenate strings using a specified separator, with the default separator being a space.
Syntax:
paste(..., sep = " ", collapse = NULL)
Parameter Description:
- ... : a list of strings
- sep : separator, default is space
- collapse : when two or more string objects are concatenated element-wise, after joining the strings, use the connector specified by collapse to join them together.
Example
b <- 'Example'
c <- "Taobao"
print(paste(a,b,c))
print(paste(a,b,c, sep = "-"))
print(paste(letters[1:6],1:6, sep = "", collapse = "="))
paste(letters[1:6],1:6, collapse = ".")
Executing the above code produces the following result:
[1] "Google Example Taobao" [1] "Google-Example-Taobao" [1] "a1=b2=c3=d4=e5=f6" [1] "a 1.b 2.c 3.d 4.e 5.f 6"
format() Function
The format() function is used to format strings. format() can be applied to strings or numbers.
Syntax:
format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))
Parameter Description:
- x : input vector
- digits : number of digits to display
- nsmall : minimum number of digits to the right of the decimal point
- scientific : set scientific notation
- width : minimum width to display by filling with blanks at the beginning
- justify : set the position; display can be left, right, center, etc.
Example
result <- format(23.123456789, digits = 9)
print(result)
# Display using scientific notation
result <- format(c(6, 13.14521), scientific = TRUE)
print(result)
# Display at least 5 digits to the right of the decimal point, fill with 0 if missing
result <- format(23.47, nsmall = 5)
print(result)
# Convert number to string
result <- format(6)
print(result)
# Width of 6 digits, add spaces at the beginning if not enough
result <- format(13.7, width = 6)
print(result)
# Left-align the string
result <- format("Example", width = 9, justify = "l")
print(result)
# Center display
result <- format("Example", width = 10, justify = "c")
print(result)
Executing the above code produces the following result:
[1] "23.1234568" [1] "6.000000e+00" "1.314521e+01" [1] "23.47000" [1] "6" [1] " 13.7" [1] "Example " [1] " Example "
nchar() Function
The nchar() function is used to count the length of strings or numeric vectors.
Syntax:
nchar(x)
Parameter Description:
- x : vector or string
Example
print(result)
Executing the above code produces the following result:
[1] 20
toupper() & tolower() Functions
The toupper() & tolower() functions are used to convert letters in strings to uppercase or lowercase.
Syntax:
toupper(x) tolower(x)
Parameter Description:
- x : vector or string
Example
print(result)
# Convert to lowercase
result <- tolower("Example")
print(result)
Executing the above code produces the following result:
[1] "EXAMPLE" [1] "example"
substring() Function
The substring() function is used to extract substrings.
Syntax:
substring(x,first,last)
Parameter Description:
- x : vector or string
- first : starting position for extraction
- last : ending position for extraction
Example
result <- substring("Example", 2, 5)
print(result)
Executing the above code produces the following result:
[1] "unoo"
String Replacement
Usegsub()function to replace specific characters or patterns in a string.
Example
new_str <- gsub("World", "R", str)
# Output: "Hello, R!"
String Splitting
Usestrsplit()function to split a string into substrings.
Example
split_str <- strsplit(str, ",")
# Output: List of 1
# [[1]]
# [1] "Hello" " World!"
The above only lists a small portion of R string operation examples. R has a large number of string processing functions and operators, such as pattern matching, case conversion, string comparison, etc. You can consult the official R documentation to obtain a more complete list of string operation functions and usage instructions.
Other Extensions