Working with objects in R

Working with objects in R

2020, Sep 19    

Welcome! This is a journey into the unknown for me, like in the picture above. I’m stil relative new to data science, let alone publshing my work through GitHub pages. I will keep things very simple in this first tutorial about the basics with R. The objective is to get familiar with the syntax and the way objects are constructed. This is also a tutorial for me since I am just getting used to working with R Markdown and Github simultaneously.

Part 1: Basic Calculations

In this section we will use R as a simple calculator and also introduce a few basic built-in functions:

#first we will perform basic math:
2+2
## [1] 4
6-1
## [1] 5
4*8
## [1] 32
#we can also perform more complex operations using ():
((81/9)^2)+20
## [1] 101
#now we will use some basic functions, starting with the square root:
sqrt(64)
## [1] 8
#note that in R the syntax is quite direct, we call the function(argument)
#for ln(36) we do:
log(36)
## [1] 3.583519
#suppose we wanted to change the base to find log(36), i.e. base 10:
log(36,10)
## [1] 1.556303

Part 2: Working with Vectors

In this section we will use R to construct and manipulate vectors, which are essentially data objects with one dimension.

#first we will create a vector called x which takes these numbers: 
x <- c(45,31,60,80,32)
#to see the contents of the vector we simply call for x:
x
## [1] 45 31 60 80 32
#to find the mean we can use the function mean():
mean(x)
## [1] 49.6
#for the standard deviation we simply do sd():
sd(x)
## [1] 20.67124
#now suppose we want to create another vector called y:
y <- c(28,90,45,60,70)
#we can add the elements in their given order of these vectors by doing:
x+y
## [1]  73 121 105 140 102
#the same works with multiplication:
x*y
## [1] 1260 2790 2700 4800 2240
##finally, we can create the vector z which adds x and y and get its mean:
z <- x+y
mean(z)
## [1] 108.2

Part 3: Working with Matrices

For this last section we will work with matrices using R. The logic is a bit different since matrices have their own special codes. In this section we will perform a bit of basic linear algebra which can serve for more complex analysis:

#first we will create matrix a 2x3 matrix called a:
a <- matrix(c(1,4,2,5,3,6), nrow = 2)
#the imput order matters and also the number of rows:
a
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
#next we will create a 3x2 matrix called b:
b <- matrix(c(6,-1,8,23,7,9), nrow = 3)

#to perform matrix multiplication we imput:
a%*%b
##      [,1] [,2]
## [1,]   28   64
## [2,]   67  181

Note that the order of the matrices is important.

#for more complex operations we can create a 3x3 matrix called c:
c <- matrix(c(1,2,9,8,5,4,10,11,12), nrow = 3)
#to find the inverse of c we use solve():
solve(c)
##             [,1]       [,2]        [,3]
## [1,]  0.06504065 -0.2276423  0.15447154
## [2,]  0.30487805 -0.3170732  0.03658537
## [3,] -0.15040650  0.2764228 -0.04471545
#to find the determinant of c we use det():
det(c)
## [1] 246

Conclusions

I hope this first post was useful if you have never used R before and were curious about the language. In the next post I will redo this exercise using Python and highlight a few key differences in the syntax. Thank you for coming along on the start of this journey!