Sei sulla pagina 1di 4

Data Structures in R

Different data structures for storing a collection of data values in RAM. The
most common data structures in R are: Vectors, Factors, Matrices, Data Frames,
Lists, Arrays.

They can be categorized as 1-dimensional (vector and list), 2-dimensional


(matrix and data frame) or multidimensional (array).

They also differ according to homogeneity of elements they can contain: while
all elements contained in vector, matrix and array must be of the same type, list
and data frame can contain multiple types.

Vectors

A collection of values that all have the same data type. The elements of a vector
are all numbers, giving a numeric vector, or all character values, giving a
character vector.

A vector can be used to represent a single variable in a data set.

A vector is a one-dimensional data structure and all of its elements are of the
same data type.

> numbers <- c(10, 20, 30, 40, 50)

> numbers[1]

[1] 10

> numbers[1:4]

[1] 10 20 30 40

> numbers[c(2, 4)]

[1] 20 40

> chars <- c("a", "b", "c")

> bools <- c(TRUE, FALSE, TRUE, FALSE)

Factors
A collection of values that all come from a fixed set of possible values. A factor
is similar to a vector, except that the values within a factor are limited to a fixed
set of possible values.

A factor can be used to represent a categorical variable in a data set.

A factor is one-dimensional and every element must be one of a fixed set of


values, called the levels of the factor.

gender <-c(Male", Female, Female, Male)

status <-c(Poor, Improved Excellent, Poor , Excellent)

factor_gender <-factor(gender) # factor_genter has two levels, Male and


Female.

factor_status <-factor(status) # factor_status has three levels, Poor, Improved,


and Excellent.

Matrices

A two-dimensional collection of values that all have the same type. The values
are arranged in rows and columns.

A matrix is a two-dimensional data structure and all of its elements are of the
same type.

> vector <- c(1:4)

> foo <- matrix(vector, nrow = 2, ncol = 2)

> foo

[,1] [,2]

[1,] 1 3

[2,] 2 4

> foo <- matrix(vector, nrow = 2, ncol = 2, byrow = TRUE)

> foo

[,1] [,2]

[1,] 1 2
[2,] 3 4

mat <-matrix(c(1:4), nrow=2,ncol = 2)

mat[1,]# returns the first row in the matrix.

mat[2,]# returns the second row in the matrix.

mat[,1]# returns the first column in the matrix.

mat[,2]# returns the second column in the matrix.

mat[1,2]# returns the element in the first row of the second column.

Arrays

There is also an array data structure that extends this idea to more than two
dimensions.

a <-matrix(c(1,1,1,1) , 2, 2)

b <-matrix(c(2,2,2,2) , 2, 2)

foo <-array(c(a,b), c(2,2,2))

Data frames

A collection of vectors that all have the same length. This is like a matrix,
except that each column can contain a different data type.

A data frame can be used to represent an entire data set.

Internally a data frame is a list of vectors with equal length.

A data frame is two-dimensional and different columns may contain different


data types, though all values within a column must be of the same data type and
all columns must have the same length.

name <-c( joe , jhon , Nancy )

sex <-c(M, M, F)

age <-c(27,26,26)

foo <-data.frame(name,sex,age)

foo$name# returns the name vector in the data frame


foo$age# returns the age vector in the data frame

foo$age[2]# returns the second element of the age vector in the data frame

foo[1, ]

foo[, 1]

Lists

A collection of data structures. The components of a list can be simply vectors--


similar to a data frame, but with each column allowed to have a different length.
However, a list can also be a much more complicated structure.

Lists may include vectors, arrays, matrices, data frames and other lists.

This is a very flexible data structure. Lists can be used to store any combination
of data values together.

A list is a hierarchical data structure and each component of a list may be any
type of data structure whatsoever.

vec <-c(1,2,3,4)

mat <-matrix(vec,2,2)

foo <-list(vec, mat)

> foo[[1]]

[1] 1 2 3 4

> foo[[2]]

[,1] [,2]

[1,] 1 3

[2,] 2 4

> foo[[2]][1,]

[1] 1 3

Potrebbero piacerti anche