R Strings
In R, a string is a data type representing text data. It consists of characters (a character vector) and can include letters, digits, symbols, spaces, and other characters.
R strings can be represented by 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 a string:** You can create a string using single or double quotes.
The following example demonstrates the use of strings:
Example
a
<-
'Using single quotes'
(
a
)
b
<-
"Using double quotes"
(
b
)
c
<-
"A double-quoted string can contain single quotes (') "
(
c
)
d
<-
'A single-quoted string can contain double quotes (") '
(
d
)
The output of the code above is:
[1] "使用单引号"
[1] "使用双引号"
[1] "双引号字符串中可以包含单引号(') "
[1] "单引号字符串中可以包含双引号(\") "
String Operations
R provides a variety of functions and operators for manipulating strings, making it convenient to process and handle text data.
Below we look at some of R's built-in functions for string operations.
The paste() function
The paste() function is used to concatenate strings with a specified separator; the default separator is a space.
Syntax format:
paste(..., sep = " ", collapse = NULL)
Parameter Description:
- ... : A list of strings
- sep : The separator, a space by default
- collapse : Two or more string objects are joined together according to their corresponding elements; after the strings are concatenated, the connector specified by collapse is used to join them
Example
a
<-
"Google"
b
<-
'Runoob'
c
<-
"Taobao"
(
paste
(
a,b,
c
)
)
(
paste
(
a,b,
c
, sep
=
"-"
)
)
(
paste
(
letters
[
1
:
6
]
,
1
:
6
, sep
=
""
, collapse
=
"="
)
)
paste
(
letters
[
1
:
6
]
,
1
:
6
, collapse
=
"."
)
The output of the code above is:
[1] "Google Runoob Taobao"
[1] "Google-Runoob-Taobao"
[1] "a1=b2=c3=d4=e5=f6"
[1] "a 1.b 2.c 3.d 4.e 5.f 6"
The format() function
The format() function is used to format strings; format() can be applied to strings or numbers.
Syntax format:
format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))
Parameter Description:
- x : The input vector
- digits : The number of digits to display
- nsmall : The minimum number of digits to display to the right of the decimal point
- scientific : Sets scientific notation
- width : The minimum width to display, achieved by padding with spaces at the beginning
- justify: Sets the alignment; the display can be left, right, center, and so on.
Example
Display 9 digits, with the last one rounded
result
<-
format
(
23.123456789
, digits
=
9
)
(
result
)
Display using scientific notation
result
<-
format
(
c
(
6
,
13.14521
)
, scientific
=
TRUE
)
(
result
)
Display at least 5 digits to the right of the decimal point, padding with 0 where needed
result
<-
format
(
23.47
, nsmall
=
5
)
(
result
)
Convert a number to a string
result
<-
format
(
6
)
(
result
)
A width of 6 digits; add spaces at the beginning if it is shorter
result
<-
format
(
13.7
, width
=
6
)
(
result
)
Left-align the string
result
<-
format
(
"Runoob"
, width
=
9
, justify
=
"l"
)
(
result
)
Center the display
result
<-
format
(
"Runoob"
, width
=
10
, justify
=
"c"
)
(
result
)
The output of the code above is:
[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] " 13.7"
[1] "Runoob "
[1] " Runoob "
The nchar() function
The nchar() function is used to count the length of a string or a list of numbers.
Syntax format:
nchar(x)
Parameter Description:
- x : A vector or string
Example
result
<-
nchar
(
"Google Runoob Taobao"
)
(
result
)
The output of the code above is:
[1] 20
The toupper() & tolower() functions
The toupper() & tolower() functions convert the letters of a string to uppercase or lowercase.
Syntax format:
toupper(x)
tolower(x)
Parameter Description:
- x : A vector or string
Convert to uppercase
Example
result
<-
toupper
(
"Runoob"
)
(
result
)
Lowercase conversion
result
<-
tolower
(
"Runoob"
)
(
result
)
The output of the code above is:
[1] "RUNOOB"
[1] "runoob"
The substring() function
The substring() function is used to extract part of a string.
Syntax format:
substring(x,first,last)
Parameter Description:
- x : A vector or string
- first : The starting position of the extraction
- last: The ending position of the extraction
Example
Extract from position 2 to position 5
result
<-
substring
(
"Runoob"
,
2
,
5
)
(
result
)
The output of the code above is:
[1] "unoo"
String replacement
Use 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 strsplit() Function that splits a string into substrings.
Example
str
<-
"Hello, World!"
split_str
<-
strsplit
(
str
,
","
)
Output: List of 1
[[1]]
[1] "Hello" " World!"
The above lists only 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, and string comparison. You can consult R's official documentation for a fuller list and usage notes on string functions.