ATA Tructures In: Pavan Kumar A
ATA Tructures In: Pavan Kumar A
Pavan Kumar A
DATA STRUCTURES IN R
Types of data structures in R
Vector : It is the structure that can contain one or more values of a single
type or mixed (characters, integers)
It is represented as one dimensional data
c() examples
> c(42,57,12,39,1,3,4)
[1] 42 57 12 39 1 3 4
You can also concatenate vectors of more than one element
> x <- c(1, 2, 3)
> y <- c(10, 20)
DATA STRUCTURES IN R- INTEGER VECTORS
seq(): It is used to generate the series of numbers which is of equidistant
It accepts three arguments
Start element
Stop element
Jump element
> seq(4,9)#It generates the numbers from 4 to 9, only 2 arguments are given
[1] 4 5 6 7 8 9
If you want to insert quotes with in the string, the \” is used. For example
> cat("What is \"R\"?\n")
What is "R"?
DATA STRUCTURES IN R- CHARACTER VECTORS
Logical vectors can take the value TRUE or FALSE
In input, you may use the convenient abbreviations T and F
> c(T,T,F,T)
[1] TRUE TRUE FALSE TRUE
DATA STRUCTURES IN R- CHARACTER VECTORS
Example of Character Vector: Indexing
DATA STRUCTURES IN R- CHARACTER VECTORS
Missing values
In many data sets, you may find missing values.
We need to have some method to deal with the missing values
R allows vectors to contain a special NA value.
Arrays are similar to matrices but can have more than two dimensions.
See help(array) for details
DATA STRUCTURES IN R- MATRIX
Subsetting a matrix Example
We can extract the
elements from the
matrix – Matrix
Subsetting.
Since it is a two
dimensional
representation of
numbers, we can
access it with two-
dimensional accessor
[,]
DATA STRUCTURES IN R- MATRIX
Matrix Operations
Addition
Substraction
Exp
Element-wise *
Mat Mult %*%
rowsums()
rowmeans()
colsums()
colmeans()
t()
DATA STRUCTURES IN R - ARRAYS
Arrays
It is a vector that is represented and accessible in a given number of
dimensions (mostly more than two dimensions).
DATA STRUCTURES IN R-LISTS
Each column in the Data Frame can be a separate type of data. In the previous
example „mydata‟ data frame, it is the combination of numerical, character and
factor data types.
ACCESSING DATA FRAMES
There are a variety of ways to identify the elements of a data frame. Here are
few screenshots.
BUILD-IN DATA FRAMES IN R
R has some build-in datasets. „mtcars‟ is one datasets
CREATING DATA SUBSETS
R deals with huge data, not all of which is useful.
Therefore, first step is to sort out the data containing the relevant information.
Extracted data sets are further divided into small subsets of data.
#Using subset function Creates the subset of numbers greater than 4 using
subset(v,v<4) subset() function