Hello everyone, let us dive into the basics of R programming:
Before getting started with R, download the necessary files like R Studio and R. But don't worry because I already wrote a blog on how to install R on macOS and Windows. The link to the blog is mentioned below:
https://dev.to/ruthvikraja_mv/installing-r-studio-on-mac-59hc
This article is a continuation to my previous Section 1 on R.
[https://dev.to/ruthvikraja_mv/core-programming-principles-in-r-4nkk]
The R code is as follows:
################################# SECTION 2 ########################################
## Fundamentals of R ##
# R is a statistical programming language
# What is a vector in R?
# A vector in R is similar to an array like in other programming languages[C, C++ etc]
# A vector in R is a sequence of data elements and the numbering starts from 1 but in other
# programming languages the numbering starts from 0
# A Numeric vector in R consists of a sequence of numeric values
# A Character vector in R consists of a sequence of characters and in R only the similar data types
# of data elements can be stored in a vector[Note: One data element can consists of multiple characters
# i.e like a "string" and it is considered as a single character]
# Remember: In R, if we try to store a single value in a variable it is stored in the form of a vector
## Let us create some vectors:-
a<-c(3,45,6,7,8) # The c function in R programming stands for "combine"
a
class(a)
is.numeric(a) # Returns TRUE because a is of Numeric type
is.integer(a) # Returns FALSE because by default the values are stored as double
is.double(a) # Returns TRUE
b<-c(3L, 4L, 5L)
is.integer(b) # Returns TRUE
b[1]
c<-c(1, "a", 1) # A vector can only have similar data types but when we try to send a numeric data type
c # it is converted to character data type automatically
class(c)
# Let us explore some inbuilt functions:
# seq(start, end) -> To create a sequence of numbers, it is like ":"
# rep() -> Replicate a value for a particular number of times
seq(1,16)
# OR #
1:16
# But what is difference between sequence function and ":" ?
# In sequence function we can mention the step value as below mentioned:
z<-seq(1,16,2)
z
rep(1,2) # Replicating value 1 for 2 times
# We can also replicate characters and vectors:
rep("a",6) # Replicating a character
rep(c(1,2,3,4),3) # Replicating a vector
# Accessing individual elements in a vector:
w<-c("a","b","c","d","e")
w[1] # The indexing starts from 1 in R language
# In R if I want all the elements except the 1st element then the following command is used:
w[-1] # whereas, in python if we try to execute the same command then it prints the last value
# Also try:
w[-2] # In python this prints the last but one element
w[1:3] # prints elements from 1st to 3rd position
# Also try:
w[c(1,3,5)] # prints the 1st, 3rd and 5th value
-3:-5 # prints values starting from -3 to -5
w[-3:-5] # Thereby, this excludes values in positions 3, 4 and 5
w[7] # prints NA
## Vectorized Operations:
# Adding two vectors element wise:
# Addition, subtraction, multiplication, comparisions etc can be done in R without looping
# through each element in a vector
# Ex:
a<-c(1,2,3,4)
b<-c(5,6,7,8)
c<-a+b
c
## Recycling of vectors:
# when you try to add a vector of size 5 elements with a vector of size 10 elements,
# R will reuse the 5 elements of first vector to add with the last 5 elements of the 2nd
# vector
a<-c(1,2,3,4,5)
b<-c(1,2,3,4,5,6,7,8,9,0)
c<-a+b
c
# In R programming, vectors can be sent as arguments to the functions and also functions can
# return vectors
## The Power of vectorized operations:
x<-rnorm(5) # Initializing five random numbers
x
for(i in x){
print(i)
}
# OR #
# Conventional programming loop
for(j in 1:5){
print(x[j])
}
N<-1000000
a<-rnorm(N)
b<-rnorm(N)
# Vectorized approach
c<-a*b
# De-vectorized approach
d<-rep(NA, N) # creating a empty vector with null values
for(i in 1:N){
d[i]<-a[i]*b[i]
}
# Thereby from above it is clear that vectorized approach is much shorter and simple also
# vectorized approach consumes less time the de-vectorized approach
# To know more about a particular function just type ? at starting of the function, as follows:
# Ex:
?seq()
## Packages in R
# To install a package in R use the following command (or) navigate to the packages tab to install the
# package:
install.packages("ggplot2") # ggplot2 package is used for graphical representations in R
library(ggplot2) # This is done to activate the package
# Ex:
qplot(data=diamonds, carat, price, colour=clarity, facets=.~clarity)
Thank you, for spending your time on my post. Follow me for more updates on R.
The next post on R would be on the topic Matrices[Section 3]
Happy coding…
Top comments (0)