Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Lect-13 Data Structures in R Programming

The document provides an overview of data structures in R programming, focusing on their organization and types, including strings, vectors, lists, dataframes, matrices, arrays, and factors. It details how to create, manipulate, and access these structures, particularly emphasizing string operations such as creation, length determination, case conversion, concatenation, and updating. Additionally, it covers vector creation, types, accessing elements, modifying, deleting, and sorting vectors.

Uploaded by

rajmahrolia2003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lect-13 Data Structures in R Programming

The document provides an overview of data structures in R programming, focusing on their organization and types, including strings, vectors, lists, dataframes, matrices, arrays, and factors. It details how to create, manipulate, and access these structures, particularly emphasizing string operations such as creation, length determination, case conversion, concatenation, and updating. Additionally, it covers vector creation, types, accessing elements, modifying, deleting, and sorting vectors.

Uploaded by

rajmahrolia2003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Data Structures in R Programming

• A data structure is a particular way of organizing data


in a computer so that it can be used effectively.
• The idea is to reduce the space and time complexities
of different tasks.
• Data structures in R programming are tools for holding
multiple values.
• R’s base data structures are often organized
-- by their dimensionality (1D, 2D, or nD) and
--whether they’re homogeneous or heterogeneous
(i. e. all elements are of the identical type or
the elements are often of various types).
Seven data structures most frequently utilized in data analysis

• 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!"

Multiple strings can be updated at once, with


the start index and end index.
•If the length of the substring is larger than
the new string, only the portion of the
substring equal to the length of the new
string is replaced.
•If the length of the substring is smaller than
the new string, the start index to end index
positions of the substring is replaced with the
corresponding new string values.
R Vectors
• R Vectors are the same as the arrays in R
language which are used to hold multiple
data values of the same type.
• One major key point is that in
R Programming Language the indexing of
the vector will start from ‘1’ and not from
‘0’.
• We can create numeric vectors and
character vectors as well.
Creating a vector

• A vector is a basic data structure that


represents a one-dimensional array.

• To create a vector we use the “c” function


which the most common method use in R
Programming Language.
# R program to create Vectors

# using the c function to combine the values as a vector.


# By default the type will be double
X<- c(61, 4, 21, 67, 89, 2)
cat('using c function', X, '\n')
# seq() function for creating a sequence of continuous
values.
# length.out defines the length of vector
Y<- seq(1, 10, length.out = 5)
cat('using seq() function', Y, '\n')
# use':' to create a vector of continuous values.
Z<- 2:7
cat('using colon', Z)
Output:
using c function 61 4 21 67 89 2
using seq() function 1 3.25 5.5 7.75 10
using colon 2 3 4 5 6 7
Types of R vectors
Vectors are of different types which are
used in R. Following are some of the types
of vectors:

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.

Sorting elements of a R Vector


sort() function is used to sort the values in
ascending or descending order.
# R program to modify elements of a Vector
X<- c(2, 7, 9, 7, 8, 2)
# modify a specific element
X[3] <- 1
X[2] <-9
cat('subscript operator', X, '\n')
# Modify using different logics.
X[1:5]<- 0
cat('Logical indexing', X, '\n')
# Modify by specifying the position or elements.
X<- X[c(3, 2, 1)]
cat('combine() function', X)
subscript operator 2 9 1 7 8 2
Logical indexing 2 9 0 0 0 2
combine() function 0 9 2
# R program to delete a Vector
M <- c(8, 10, 2, 5)
# set NULL to the vector
M <- NULL Output:
cat('Output vector', M) Output vector NULL

# R program to sort elements of a Vector


# Creation of Vector Output:
X<- c(8, 2, 7, 1, 11, 2) ascending order
# Sort in ascending order 1 2 2 7 8 11
A <- sort(X) descending order
cat('ascending order', A, '\n') 11 8 7 2 2 1
# sort in descending order by setting decreasing as TRUE
B <- sort(X, decreasing = TRUE)
cat('descending order', B)

You might also like