Objects and Types

Below is a video on objects and types in R.  If you would prefer to read this information, just skip the video!

 

Numbers, letters, and other characters can be used, saved, and defined in different ways in R. These characters can be defined to be objects. Objects can be defined to be a vector or matrix of data. Then these data can be defined to be different types such as numeric or a character. Let us delve into these new ideas step by step to get a better understanding of what they are and mean.

Objects

Objects are what you store data into. Two of the most common ways are by vectors and matrices. You can think of a vector or a matrix as an excel spreadsheet of data. You can have just one column or row filled with data. That is essentially what a vector is. If you are a mathematician or a statistician, you might describe a vector to be one dimensional. Below is an example of this excel in Figure 1. The code below that is the data imported into R, then displayed. It uses packages and functions, topics we have not discussed yet. To read more about them, click here for packages and here for functions. Figure 2 shows the output for this code in R. The download link for the file is below Figure 2.

Figure 1
Figure 1
#code 1 - importing vector from excel 

#we are assuming that the file has been placed into your working directory

data1<-read.table('objects and types file 1 vector excel.csv') #reading in the excel file - it is a comma delimited test file or .csv

data1<-as.matrix(data1) #converts from data frame to matrix

data1<- as.vector(data1) #converts from matrix to vector

data1 #display the data in vector form
Figure 2
Figure 2

Vector in Excel File – .csv format

Or you can have data in multiple rows and columns with or without headers and descriptive labels. This is what a matrix is like. In essence, a matrix is a bunch of vectors put together. If you are a mathematician or a statistician, you might describe a matrix to be multi-dimensional. Below is an example of this in Excel in Figure 3. The code below that is the data imported into R, then displayed. Figure 4 shows the output for this code in R. The download link for the file is below Figure 4.

Figure 3
Figure 3
#code 2 - importing matrix from excel

#we are assuming that the file has been placed into your working directory

data2<-read.table('objects and types file 2 matrix excel.csv',  sep=",") #reading in the excel file - it is a comma delimited test file or .csv

data2<-as.matrix(data2) #converts from data frame to matrix

data2
Figure 4
Figure 4

Matrix in Excel File – .csv format

Types

There are three data types; numerical, character, and logical. We will primarily focus on numerical and character for the sake of brevity, but we will briefly discuss logical.

Numerical

Numerical data types are the most straightforward to initially learn. They are simply numbers that have the ability to have mathematical operations performed on them. Here is an example of a numerical vector and matrix. The output in R is below in Figure 5.

#code 3 - numerical vector and matrix

vector1<-c(1,2,3,4) #numerical vector
vector1 #displaying the vector

matrix1<- matrix(c(1,2,3,4), nrow=2) #numerical matrix 2 by 2
matrix1 #displaying the matrix
Figure 5
Figure 5

Character

Character data types are very similar to numerical data in appearance. However, mathematical operations cannot be performed on these. However, there are cases where a matrix or table of character values will be more helpful. For instance, it is easier to count duplicated values when the matrix is full of character data instead of numerical data.

Character data can be defined as a number of letters that is enclosed by quotation marks or an apostrophe. ‘ ’ and “ ” are equivalent and both work. On the other hand, ‘ “ will not work. They must both be quotation marks or apostrophes. Let us see an example, as this is be clearer. The code of a character vector and matrix is shown below. The output in R is below in Figure 6.

#code 4 - character vector and matrix

vector2<-c('1','2','3','4') #character vector
vector2 #displaying the vector

matrix2<- matrix(c('1','2','3','4'), nrow=2) #numerical matrix 2 by 2
matrix2 #displaying the matrix
Figure 6
Figure 6

Logical

Logical data is either TRUE or FALSE, and it is displayed in all capital letters as such. Here is an example of a logical vector.

#code 5 - logical vector

vector3<- c(TRUE, FALSE, TRUE, FALSE, FALSE, TRUE)#logical vector
vector3 #displaying vector3
Figure 7
Figure 7