Lect-13 Data Structures in R Programming
Lect-13 Data Structures in R Programming
• Strings
• Vectors
• Lists
• Dataframes
• Matrices
• Arrays
• Factors
R Strings:
• Strings are a bunch of character variables.
• It is a one-dimensional array of characters.
• One or more characters enclosed in a pair of matching
single or double quotes can be considered a string in
R.
• Strings in R Programming represent textual content
and can contain numbers, spaces, and special
characters.
• An empty string is represented by using “.
• R Strings are always stored as double-quoted values.
• A double-quoted string can contain single quotes
within it.
• Single-quoted strings can’t contain single quotes.
• Double quotes can’t be surrounded by double quotes.
Creation of String in R
• R Strings can be created by assigning character values to a
variable.
• These strings can be further concatenated by using various
functions and methods to form a big string.
# R program for String Creation
# creating a string with double quotes
str1 <- "OK1"
cat ("String 1 is : ", str1)
# creating a string with single quotes
str2 <- 'OK2'
cat ("String 2 is : ", str2)
str3 <- "This is 'acceptable and 'allowed' in R"
cat ("String 3 is : ", str3)
str4 <- 'Hi, Wondering "if this "works"'
cat ("String 4 is : ", str4)
str5 <- 'hi, ' this is not allowed'
cat ("String 5 is : ", str5)
Output
String 1 is: OK1
String 2 is: OK2
String 3 is: This is 'acceptable
and 'allowed' in R
String 4 is: Hi, Wondering "if
this "works"
Error: unexpected symbol in " str5
<- 'hi, ' this"
Execution halted
1. Length of String
The length of strings indicates the
number of characters present in the string.
The function
str_length() belonging to the ‘string’ package or
nchar() inbuilt function of R can be used to
determine the length of strings in R.
2. Accessing portions of an R string
The individual characters of a string can be
extracted from a string by using the indexing
methods of a string. There are two R’s inbuilt
functions in order to access both the single
character as well as the substrings of the string.
substr() or substring() function in R extracts
substrings out of a string beginning with the start
index and ending with the end index.
It also replaces the specified substring with a new
# R program for finding length of string
# Importing package
library(stringr)
# Calculating length of string Output
str_length("hello") [1] 5
nchar("hel'lo") [1] 6
# R program to access characters in string
str <- "Learn Code"
# counts the characters in the string
len <- nchar(str)
# Accessing character using substring() function
print (substring(str, len, len))
# Accessing elements out of index
print (substring(str, len+1, len+1))
print(substr(str, 1, 4))
print(substr(str, len-2,len)
The number of characters in the string is 10.
The first print statement prints the last
character of the string, “e”, which is str[10].
The second print statement prints the 11th
character of the string, which doesn’t exist,
but the code doesn’t throw an error and print
“”, that is an empty character.
Output
[1] "e"
[1]"Lear"
[1]"ode"
3. Case Conversion
The R string characters can be converted to
upper or lower case by R’s inbuilt function.
toupper() which converts all the characters to upper
case, tolower() which converts all the characters to
lower case, and casefold(…, upper=TRUE/FALSE)
which converts on the basis of the value specified to
the upper argument.
# R program to Convert case of a string
str <- "Hi LeArn CodiNG"
print(toupper(str))
print(tolower(str))
Outputupper = TRUE))
print(casefold(str,
[1] "HI LEARN
CODING"
[1] "hi learn
coding"
[1] "HI LEARN
By default, the value of upper
CODING"
in casefold() function is set to FALSE.
If we set it to TRUE, the R string gets
printed in upper case.
4. Concatenation of R Strings
Using R’s paste function, you
can concatenate strings.
# Create two strings
string1 <- "Hello"
string2 <- "World"
# Concatenate the two strings
result <- paste(string1, string2)
# Print the result
print(result)
result <- paste("Hello", "to",
"the World") Output
print(result) "Hello World”
"Hello to the World"
5. R String formatting
String formatting in R is done via the
sprintf function.
# Create two strings
string1 <- "Hello“
# Create two variables with values
x <- 42
y <- 3.14159
# Format a string with the two variable values
result <- sprintf("The answer is %d, and pi is %.2f.",
x, y)
Output
"The answer is 42, and pi is 3.14."
6. Updating R String
The characters, as well as substrings of a
string, can be manipulated to new string values.
The changes are reflected in the original string.
In R, the string values can be updated in the
substr way:
following (..., start, end) <-
newstring
substring (..., start, end) <-
newstring
# Create a string
string <- "Hello, World!“
# Replace "World" with "Universe“
string <- gsub("World", "Universe", string)
# Print the updated string
print(string)
newstring <- "Universe“
substr (string, 3, 11) <- newstring
print(string)
substring (string, 5, 6) <- newstring
print(string)
[1] "Hello, Universe!"
[1] "Universe"
[1] "HeUniverseverse!"
[1] "HeUnUnerseverse!"
Numeric vectors
Character vectors
Logical vectors
# R program to create numeric Vectors
v1<- c(4, 5, 6, 7)
typeof(v1)
v2<- c(1L, 4L, 2L, 5L)
typeof(v2)
v1<- c('geeks', '2', 'hello', 57)
typeof(v1)
v1<- c(TRUE, FALSE, TRUE, NA)
typeof(v1)
x <- as.raw(c(0x1, 0x2, 0x3, 0x4,
0x5))
typeof(x)
x <- c(2 + 3i, 3.56 + 1i, 8, 2i)
Print(x)
typeof(x)
[1] "double"
[1] "integer"
[1] "character“
[1] "logical"
[1] "raw"
[1] 2.00+3i 3.56+1i 8.00+0i 0.00+2i
[1] "complex"
Length of R vector
• In R, the length of a vector is determined
by the number of elements it contains.
• length() function is used to retrieve the
length of a vector.
Accessing R vector elements
• Accessing elements in a vector is the process of
performing operation on an individual element of
a vector.
• There are many ways through which we can
access the elements of the vector.
• The most common is using the ‘[ ]’, symbol.
# R program to access elements of a Vector
# accessing elements with an index number.
X<- c(2, 5, 18, 1, 12)
cat('Using Subscript operator', X[2], '\n')
# by passing a range of values inside the vector
index.
Y<- c(4, 8, 2, 1, 17)
cat('Using combine() function', Y[c(4, 1)], '\n')
print(Y[4:1])
print(Y[1:4])
Using Subscript operator 5
Using combine() function 1 4
[1] 1 2 8 4
[1] 4 8 2 1
Modifying a R vector
• Modification of a Vector is the process of
applying some operation on an individual
element of a vector to change its value in the
vector.
• There are different ways through which we can
modify a vector:
Deleting R vector elements
• Deletion of a Vector is the process of deleting all
of the elements of the vector.
• This can be done by assigning it to a NULL value.