R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Strings are sequences of characters, and they are one of the fundamental data types in many programming languages, including R. In R, strings are represented using either single (' '
) or double (" "
) quotes.
In this tutorial, we will cover:
You can create a string by enclosing text in quotes:
str1 <- "Hello, World!" str2 <- 'Hello, R!'
In strings, certain characters can't be used directly. For these, you'll use escape sequences:
# Newline character print("Hello\nWorld") # Tab character print("Hello\tWorld") # Double quote inside a string enclosed by double quotes print("Hello \"World\"!")
You can concatenate (combine) two strings using the paste()
function:
str1 <- "Hello" str2 <- "World" combined_str <- paste(str1, str2) print(combined_str) # Outputs: "Hello World"
To find the length of a string, use the nchar()
function:
str <- "Hello" length_str <- nchar(str) print(length_str) # Outputs: 5
To extract a substring from a string, use the substr()
function:
str <- "Hello World" sub_str <- substr(str, start = 1, stop = 5) print(sub_str) # Outputs: "Hello"
You can change the case of a string using toupper()
and tolower()
:
str <- "Hello World" print(toupper(str)) # Outputs: "HELLO WORLD" print(tolower(str)) # Outputs: "hello world"
Split a string into parts based on a delimiter using the strsplit()
function:
str <- "Hello,World,How,Are,You" split_str <- strsplit(str, split = ",") print(split_str) # Outputs: "Hello" "World" "How" "Are" "You"
Use the gsub()
function to replace substrings:
str <- "I like R." new_str <- gsub("R", "R programming", str) print(new_str) # Outputs: "I like R programming."
Trim whitespace from the start and end of a string using the trimws()
function:
str <- " Hello World " trimmed_str <- trimws(str) print(trimmed_str) # Outputs: "Hello World"
Strings are an essential data type in R, with various functions available to manipulate and process them. This tutorial only scratches the surface of string operations in R. The stringr
package offers even more functionality and can be a great asset for advanced string operations.
Creating and manipulating strings in R:
# Example of creating and manipulating strings my_string <- "Hello, R!" str_length <- nchar(my_string) uppercase_string <- toupper(my_string)
String manipulation functions in R:
# Example of string manipulation functions my_string <- " R Programming " trimmed_string <- trimws(my_string) lowercase_string <- tolower(trimmed_string)
Subsetting and indexing strings in R:
# Example of subsetting and indexing strings my_string <- "Data Science" first_char <- my_string[1] substring <- substr(my_string, start = 6, stop = 11)
R code examples for working with character vectors:
# Example of working with character vectors names <- c("Alice", "Bob", "Charlie") name_length <- nchar(names)
String concatenation in R:
paste()
or paste0()
functions.# Example of string concatenation first_name <- "John" last_name <- "Doe" full_name <- paste(first_name, last_name)
String matching and searching in R:
grep()
or grepl()
.# Example of string matching and searching names <- c("Alice", "Bob", "Charlie") matching_names <- grep("li", names, value = TRUE)
Regular expressions for strings in R:
# Example of using regular expressions text <- c("apple", "banana", "cherry") pattern <- "^b" matching_strings <- grep(pattern, text, value = TRUE)
Formatting and parsing strings in R:
sprintf()
for formatting and as.numeric()
for parsing.# Example of formatting and parsing strings value <- 42 formatted_text <- sprintf("The value is %d", value) numeric_string <- "123" numeric_value <- as.numeric(numeric_string)
Handling missing values in strings in R:
is.na()
or na.omit()
.# Example of handling missing values in strings names <- c("Alice", NA, "Charlie") complete_names <- na.omit(names)