The complete beginners guide to R programming
Updated: Apr 17, 2020
YOU CANNOT TEACH BEGINNERS TOP DOWN PROGRAMMING, BECAUSE THEY DON'T KNOW WHICH END IS UP. - C.A.R. Hoare
Hello folks, This is Zeeshan an IT engineer. Completed my Bachelors degree from Mumbai University.
So, This year was a great start, learned new skills and technologies, but from all of them R was quite promising, So i have written this blog for those guys out there, who are interested in learning R and just started working in R.
Before getting started, As all languages or technology demands. A small introduction to what is R? and why to use R and why is it essential to learn
R is a language as well as an environment for statistical computing. R language was developed by Bell laboratories by John Chambers and colleagues.
R Provides variety of statistical and graphical techniques which is Open source as well.
It runs on MacOs, Unix and Windows.
Why R?
Open Source.
Run on all platforms.
R is popular and increasing in popularity.
Learning R will increase the chance of getting jobs.
R is used by biggest tech giants like Google, Twitter, Microsoft.
R Environment
Effective Storage facility and data handling.
It is helpful for calculations in array, matrices.
Large Tools for data analysis.
Data analysis Result in Graphical format either on-screen or in hard copy.
R is a well developed simple and effective programming language which has conditional loops, user defined recursive functions, input and output facilities.
Application of R programming in Real world
Data Science.
Statistical computing.
Machine learning.
How to install R in Windows
Go to Official site of R programming.
Click on CRAN on the left sidebar.
Select a Mirror.
"Download R for Windows".
Click on the link that says Base.
Run file and follow the steps.
Same steps for MacOs and Ubuntu.
How to install R Studio in Windows
R Studio is the most popular IDE for running R program and its free license.
The installation is straight forward. Download R studio from R studio official site
Run the file. Follow the instructions.
NOTE: You need to first install R before installing R Studio.
Starting with R Studio
Go to files > New File > R Script
R Studio GUI is divided in 4 major sections as shown below:
R has a reputation of getting things done with very little code.
To Run a code in R studio select the line of code and press run on top right or Ctrl+ENTER
Lets take some basic examples in R studio using Vector, loops, conditions, data frames
All this are to be explained later in my blogs in details
1. Create a vector of no. from 1 to 100 using switch you will ask the user
a) 1. Display even
2. Display odd
vec<-seq(1,100)
vec1<-seq(2,100,2)
vec2<-seq(1,100,2)
print("1.Display Even")
print("2.Display Odd")
choice = as.integer(readline(prompt="Enter choice[1/2]: "))
result <- switch(choice,vec1,vec2)
print(paste(result))
Output:
2. Finding area of 5 circles using function who have any radius.
#radius of 5 circles
radius <- c(4,6,2,8,10)
area=vector()
#function to calculate area
circle<-function(){
for(i in radius)
{
a=3.14*i*i
area<-c(area,a)
}
area
}
circle()
Output:
3. Create a vector containing 7 names and print those names using while loop.
#assign names in names variable
names<-c("Zeeshan","Sameena","Nagaaaama","Shadab","arshad","Saniya","Sufiyan")
c=1 #initialize names with 1
#loop will run until c reaches the last name
while(c<=7){
print(names[c]) #printing names and incrementing c
c=c+1
}
Output:
4. Create a data frame consisting of student names, roll no and marks. To display 7 rows using repeat.
# create a dataframe
students<-data.frame(
sid=c(1:10),
sname=c("Zeeshan","Sameena","Nagaaaama","Shadab","arshad","Saniya","Sufiyan","husain","adnan","tahir"),
smarks=c(90,100,75,62,15,45,84,65,32,87)
)
#using repeat function
r=1
repeat{
print(students[r,])
r=r+1
if(r>7){
break
}
}
Output:
5. Use apply function for finding sum of rows and columns of matrix.
#create a matrix
mat <- matrix(c(5:24), nrow = 5, byrow = FALSE)
mat
#Sum of Rows
ro=vector()
ro<-apply(mat, 1, sum)
ro
#sum of columns
co=vector()
co<-apply(mat,2, sum)
co
Output:
Above are the few examples of R programming. Further details explanations of the functions, vectors, data frames will be in my other blogs. Thank you😁
Using Help() Function in R
To get help on specific topic we can use help() function along with the topic we want to search
help(syntax)
Follow R communities and blogs
Learn and get help from others and become better in R.
Some of them are:
Recommended books for R
R for Data Science
An Introduction to statistical learning with applications in R
Learning R studio for R Statistical Computing.