Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

R DATA STRUCTURES

R DATA STRUCTURES

• 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 DATA STRUCTURES

• R’s base data structures can be organised by their dimensionality


(1d, 2d, or nd) and whether they’re homogeneous (all contents
must be of the same type) or heterogeneous (the contents can be of
different types)
R DATA STRUCTURES
R VECTOR
A vector is a collection of elements, all of the same
type.

A vector are most commonly of mode character,


logical, integer or numeric.

The functions typeof(), length(), class() and str()


provide useful information about your vectors and R
objects in general.
EXAMINING VECTORS

• typeof(z)
• length(z)
• class(z)
• str(z)
ADDING ELEMENTS

• The function c() (for combine) can also be used to add


elements to a vector.
• z <- c(z, "Annette")
• z <- c("Greg", z)
VECTOR OPERATIONS

• x <-c (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)


• Addition, subtraction and division are just as easy.
• x * 3, sqrt(x)…
• create two vectors of equal length
• y <- -5:4
• x + y……..
• length(x)
COMPARISONS ALSO WORK ON
VECTORS
• x <= 5
• x > y………….
VECTORS FROM A SEQUENCE OF NUMBERS

• You can create vectors as a sequence of numbers.


• series <- 1:10
• seq(10)
• seq(from = 1, to = 10, by = 0.1)
MISSING DATA

• R supports missing data in vectors. They are represented as


NA (Not Available) and can be used for all the vector types
• x <- c(0.5, NA, 0.7)
• xx <- c(TRUE, FALSE, NA)
• x2 <- c("a", NA, "c", "d", "e")
• x1 <- c(1+5i, 2-3i, NA)
• The function is.na() indicates the elements of the vectors
that represent missing data,
• the function anyNA() returns TRUE if the vector contains
any missing values:
• x <- c("a", NA, "c", "d", NA)
• y <- c("a", "b", "c", "d", "e")
• is.na(x)
• anyNA(x)
MIX TYPES INSIDE A VECTOR?

• All values converted into one datatype


• xx <- c(1.7, "a")
• x1 <- c(TRUE, 2)
• x <- c("a", TRUE)
• is.vector(xx)
• as.numeric("1")
• as.character(1:2)
LIST

• Unlike an atomic vector, the list is not restricted to be a single mode. A list
contains a mixture of data types.

• Lists are different from atomic vectors because their


elements can be of any type, including lists.
• A list is a special type of vector. Each element can be a different
type.
• Create lists using list() or change other objects using as.list()
• They store any number of items of any type. A list can
contain all numerics or characters or a mix of the two or
data.frames or, recursively, other lists.
• list(1, 2, 3)
• list(c(1, 2, 3))
• list3 <- list(c(1, 2, 3), 3:7)
• length(list3)
ADDING ELMT

• # add a fifth element, name


• > list3[["NewElement"]] <- 3:6
• > length(list3)
• # add a fourth element, unnamed
• > list3[[4]] <- 2
• > length(list3)
• names(list3)
• x <- list(1, "a", TRUE, 1+4i)
• The content of elements of a list can be retrieved by using
double square brackets.
• x[[1]]
• Vectors can be coerced to lists as follows:
• x <- 1:10
• x <- as.list(x)
• xlist <- list(a = "Karthik Ram", b = 1:10, data =
head(mtcars))
• names(xlist)

You might also like