R String
In the R language, a string is a data type used to represent text data. It consists of characters (a character vector) and can contain letters, numbers, symbols, spaces, and other characters.
R language strings can be represented using a pair of single quotes ' ' or a pair of double quotes " ".
* A single-quoted string can contain double quotes.
* A single-quoted string cannot contain single quotes.
* A double-quoted string can contain single quotes.
* A double-quoted string cannot contain double quotes.
**Creating Strings:** You can use single quotes or double quotes to create strings.
The following examples demonstrate the use of strings:
## Example
a <-'Using Single Quotes'
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 output:
"Using Single Quotes" "Using Double Quotes" "Double-quoted strings can contain single quotes ('οΌ " "Single-quoted strings can contain double quotes ("οΌ "
* * *
## String Operations
R language provides a variety of functions and operators for operating on strings, making it convenient to process and manipulate text data.
Below, we look at some of R's built-in functions for string operations.
### paste() Function
The paste() function is used to concatenate strings using a specified separator. The default separator is a space.
Syntax:
paste(..., sep = " ", collapse = NULL)
Parameter Description:
* ... : List of strings
* sep : Separator, default is a space
* collapse : After two or more string objects are concatenated element-wise, this parameter specifies a connector to join the resulting strings together.
## Example
a <-"Google"
b <-''
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 output:
"Google Taobao" "Google--Taobao" "a1=b2=c3=d4=e5=f6" "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 display to the right of the decimal point
* scientific : Set scientific notation
* width : Display with a minimum width by padding with spaces at the beginning
* justify: Set alignment. Display can be left, right, center, etc.
## Example
# Display 9 digits, rounding the last one
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, padding with 0 if necessary
result <-format(23.47, nsmall =5)
print(result)
# Convert a number to a string
result <-format(6)
print(result)
# Width of 6 characters, padding with spaces at the beginning if needed
result <-format(13.7, width =6)
print(result)
# Left-align the string
result <-format("", width =9, justify ="l")
print(result)
# Center-align
result <-format("", width =10, justify ="c")
print(result)
Executing the above code produces the following output:
"23.1234568" "6.000000e+00" "1.314521e+01" "23.47000" "6" " 13.7" " " " "
### nchar() Function
The nchar() function is used to count the length of a string or a list of numbers.
Syntax:
nchar(x)
Parameter Description:
* x : Vector or string
## Example
result <-nchar("Google Taobao")
print(result)
Executing the above code produces the following output:
20
### toupper() & tolower() Functions
The toupper() & tolower() functions are used to convert the letters in a string to uppercase or lowercase.
Syntax:
toupper(x) tolower(x)
Parameter Description:
* x : Vector or string
# Convert to Uppercase
## Example
result <-toupper("")
print(result)
# Convert to Lowercase
result <-tolower("")
print(result)
Executing the above code produces the following output:
"" ""
### substring() Function
The substring() function is used to extract a substring.
Syntax:
substring(x,first,last)
Parameter Description:
* x : Vector or string
* first : Starting position for extraction
* last: Ending position for extraction
## Example
# Extract from the 2nd to the 5th character
result <-substring("", 2, 5)
print(result)
Executing the above code produces the following output:
"unoo"
### String Replacement
Use the **gsub()** function to replace specific characters or patterns in a string.
## Example
str<-"Hello, World!"
new_str <-gsub("World", "R", str)
# Output: "Hello, R!"
### String Splitting
Use the **strsplit()** function to split a string into substrings.
## Example
str<-"Hello, World!"
split_str <-strsplit(str, ",")
# Output: List of 1
# []
# "Hello" " World!"
The above only lists a small portion of R language 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 language documentation for a more complete list of string operation functions and usage instructions.
YouTip