R String
R language strings can be represented using a pair of single quotes ' '
or a pair of double quotes " "
.
- 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.
The following examples demonstrate the use of strings:
Example
a <- 'Using single quotes'
print(a)
b <- "Using double quotes"
print(b)
c <- "Double-quoted string can contain single quotes (') "
print(c)
d <- 'Single-quoted string can contain double quotes ("') '
print(d)
Executing the above code outputs:
[1] "Using single quotes"
[1] "Using double quotes"
[1] "Double-quoted string can contain single quotes (') "
[1] "Single-quoted string can contain double quotes (\") "
String Operations
Let's look at some of the built-in functions in R for string operations.
paste() Function
The paste() function is used to concatenate strings with a specified separator, which defaults to a space.
Syntax:
paste(..., sep = " ", collapse = NULL)
Parameters:
- ... : List of strings
- sep : Separator, default is a space
- collapse : Concatenates elements of the string after joining, using the specified connector
Example
a <- "Google"
b <- 'tutorialpro'
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 outputs:
[1] "Google tutorialpro Taobao"
[1] "Google-tutorialpro-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, and it can be applied to strings or numbers.
Syntax:
format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))
Parameters:
- x : Input vector
- digits : Number of digits to display
- nsmall : Minimum number of digits to the right of the decimal point
- scientific : Sets scientific notation
- width : Minimum width, padded with leading spaces if necessary
- justify : Sets alignment, which can be left, right, center, etc.
Example
# Display 9 digits, rounding the last one
result <- format(23.123456789, digits = 9)
print(result)
# Display in scientific notation
result <- format(c(6, 13.14521), scientific = TRUE)
print(result)
# Minimum 5 digits to the right of the decimal point, padded with zeros if necessary
result <- format(23.47, nsmall = 5)
print(result)
# Convert number to string
result <- format(6)
print(result)
# Width of 6, padded with leading spaces
result <- format(13.7, width = 6)
print(result)
# Left-justified string
result <- format("tutorialpro", width = 9, justify = "l")
print(result)
# Centered display
result <- format("tutorialpro", width = 10, justify = "c")
print(result)
Executing the above code outputs:
[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] " 13.7"
[1] "tutorialpro "
[1] " tutorialpro "
nchar() Function
The nchar() function is used to count the length of a string or a numeric list.
Syntax:
nchar(x)
Parameters:
- x : Vector or string
Example
result <- nchar("Google tutorialpro Taobao")
print(result)
Executing the above code outputs:
[1] 24
toupper() & tolower() Functions
The toupper()
and tolower()
functions are used to convert the letters of a string to uppercase or lowercase.
Syntax:
toupper(x)
tolower(x)
Parameters:
- x: Vector or string
Example
result <- toupper("tutorialpro")
print(result)
# Convert to lowercase
result <- tolower("tutorialpro")
print(result)
Executing the above code produces the following output:
[1] "TUTORIALPRO"
[1] "tutorialpro"
substring() Function
The substring()
function is used to extract a substring from a string.
Syntax:
substring(x, first, last)
Parameters:
- x: Vector or string
- first: Starting position for extraction
- last: Ending position for extraction
Example
# Extract from the 2nd to the 5th position
result <- substring("tutorialpro", 2, 5)
print(result)
Executing the above code produces the following output:
[1] "utori"