While you do programming in any programming language, you need to use various variables to store different data. Variables are however, reserved in a memory locations to store values. This implies that, once you create a variable you reserve some area in memory. Data Structures are the way of arranging data so that it can be used efficiently in a computer.
In contrast to different programming languages like C and Java, R doesn’t have variables declared as some data type. The variables are appointed with R-Objects and the knowledge form of the R-object becomes the datatype of the variable. There are many types of R-objects. The popularly used ones are
The very first type Vector we are covering in detail is present in Beginners Guide to R.
We will follow the rest of the data types.
R Data Structures: Matrix
A matrix is a vector with two attributes
• Number of rows.
• Number of columns
Matrices can be defined as 2-dimensional arrays. All columns in a matrix must have the same mode (numeric, character, etc.) and same length. A matrix can be created by using the matrix() function:
myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns, byrow=logical_value, dimnames=list( char_vector_rownames, char_vector_colnames))
We can show the name of matrix by class function and the attributes will show the dimension for matrix.
Transpose of a matrix is to convert row into column and column into row.
variable = t(matrix name)
Here we see that the matrix y has been transposed.
Trans = t(y)
Naming rows of matrix
We employ the function rownames() to change names of rows
For the columns, we want to supply a vector of different names for the four subs involved in the test, and use this to specify the colnames(X):
subs<-c(“Maths”, “English”, “Science”, “History”) colnames(X)<-subs X
Calculations on Rows Or Columns Of The Matrix
We can use subscripts to select parts of the matrix, with a blank meaning ‘all of the rows’ or ‘all the columns’. Here is the mean of the rightmost column (number 4)
calculated over all the rows (blank then comma), and the standard deviation of the bottom row,
There are some special functions for calculating summary statistics on matrices:
Adding Rows and Columns to The Matrix
Add a row at the bottom showing the column means, and a column at the right showing the row variances:
Evaluating Functions with Apply
The apply function is used for applying functions to the rows or columns of matrices or data frames. For example:
Often, you want to apply a function across one of the margins of a matrix – margin 1 being the rows and margin 2 being the columns. Here are the total row (four of them):
Here are the total columns (four of them):
apply min to rows
apply(y, 1, min)
apply max to columns
apply(y, 2, max)
One can supply your own function definition within apply like this:
The sweep function is used to ‘sweep out’ array summaries from vectors, matrices, arrays or data frames.
In this example, we want to express a matrix in terms of the departures of each value from its column mean.
matdata<- matrix(c(50,60,40,90,100, 80,50, 90,10, 80,30, 70),nrow=4)
Create a vector containing the parameters that you intend to sweep out of the matrix. Let’s say we want to compute the four column means:
Now it is straightforward to express all the data in matdata as departures from the relevant column means:
Note: The uses of margin = 2 as the second argument indicate that we want the sweep to be carried out on the columns (rather than on the rows).
R Data Structures: Arrays
Arrays are the R data objects which can store information in addition to 2 dimensions, for instance − If we tend to create an array of dimension (2, 3, 4) then it creates 4 rectangular matrices every with a pair of rows and three columns. Arrays can store solely data type.
An array is formed using the array() operate. It takes vectors as input and uses the values within the dim parameter to form an array.
dim(array) #dim is for dimensions
The vector is not a matrix and it has no (NULL) dimensional attributes. We give the object dimensions like this (say, with five rows and four columns):
Now it does have dimensions and it is a matrix:
When we look at array it is presented as a two-dimensional table (but note that it is not a table object):
Note: The values have been entered into the array in column wise sequence: this is default in R.
• Thus a vector is a one-dimensional array that lacks any dim attributes.
• A matrix is a two-dimensional array.
• Arrays of three or more dimensions do not have any special names in R. They are simply referred to as three-dimensional or five-dimensional arrays.
Here is a three-dimensional array of the first 16 lower-case letters with three matrices each of four rows and two columns:
We want to select all the letters a to h. These are all the rows and all the columns of tables 1 and 2, so the appropriate subscripts are [,,1:2]
Next, we want only the letters i to l. These are all the rows and all the columns from the third table, so the appropriate subscripts are [„3]:
Here, we want only b,d,f,h,j,l,n and p. These are the second rows of all four tables, so the appropriate subscripts are [2,,]:
Note: When we drop the whole first dimension (there is just two row in A[2,,]) the shape of the resulting matrix is altered (two rows and four columns in this example). This is a feature of R, but you can override it by saying drop = F to retain all three dimensions:
R Data Structures: List
Like an R vector, an R list can contain items of different data types. List elements are accessed using two-part names, it is indicated with the dollar sign $ in R.
A list allows you to gather a variety of (possibly unrelated) objects under one name. For example, a list may contain a combination of vectors, matrices, data frames, and even other lists.
You create a list using the list() function: mylist <- list(object1, object2, …)
Consider a list:
j <- list(name=“hue”, sal=6000, union=F)
The list components can be accessed in several ways:
Adding and Deleting List Elements
j$gender <- “Male”
We can see the union is no more to be found in the list j.
Applying Functions To Lists: lapply() and sapply() Functions
These two functions work in a similar fashion, traversing over a set of data like a list or vector, and calling the specified function for each item.
Using the lapply() and sapply() Functions
Lists can be recursive, meaning that you can have lists within lists.
R Data Structures: Data Frames
A data frame is a list, which contains the components of that list being equal-length vectors.
kids <- c(“Sam”, “Nick”, “Rits”)
ages <- c(11,10,8)
d <- data.frame(kids,ages,stringsAsFactors=FALSE)
We see the above data is matrix-like view.
Accessing Data Frames
Data frame can be accessed in multiple ways:
Other functions structure of data frame:
Names of variables:
Identifying class of a data:
head() and tail()
First few(2) observations
Last few(2) observations
rbind() and cbind() Functions
In using rbind() to add a row, the added row is typically in the form of another data frame or list.
In using cbind() to add a row, the added column is typically in the form of another data frame or list.
gender <- c(“M”,”M”,”M”,”F”)
R Data Structures: Factors
Categorical (nominal) and ordered categorical (ordinal) variables in R are called factors.
Factors are important in R as they determine how to analyze the data and present them visually.
An R factor might be viewed simply as a vector with extra information i.e. a record of the distinct values in that vector, called levels.
x <- c(1:3)
xfac <- factor(x)
Using tapply() Function
The tapply function is useful when we need to break up a vector into groups defined by some classifying factor, compute a function on the subsets, and return the results in a convenient form. You can even specify multiple factors as the grouping variable, for example treatment and sex, or team and handedness.
numbers <- c(24,28,45,36,34,26)
subject <- c(“maths”,“english”,“english”,“maths”,“english”,”maths”) tapply(numbers,subject,mean)
Hope you include these datatypes in your daily analysis.
For more interesting blogs on BigData and Analytics, please visit our website ACADGILD.