top of page
Search

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


 
  1. R is a language as well as an environment for statistical computing. R language was developed by Bell laboratories by John Chambers and colleagues.

  2. R Provides variety of statistical and graphical techniques which is Open source as well.

  3. It runs on MacOs, Unix and Windows.

Why R?

  1. Open Source.

  2. Run on all platforms.

  3. R is popular and increasing in popularity.

  4. Learning R will increase the chance of getting jobs.

  5. R is used by biggest tech giants like Google, Twitter, Microsoft.

R Environment

  1. Effective Storage facility and data handling.

  2. It is helpful for calculations in array, matrices.

  3. Large Tools for data analysis.

  4. Data analysis Result in Graphical format either on-screen or in hard copy.

  5. 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

  1. Data Science.

  2. Statistical computing.

  3. Machine learning.

How to install R in Windows

  1. Go to Official site of R programming.

  2. Click on CRAN on the left sidebar.

  3. Select a Mirror.

  4. "Download R for Windows".

  5. Click on the link that says Base.

  6. 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

  1. Hands-on programming in R

  2. R for Data Science

  3. The Art of R Programming

  4. An Introduction to statistical learning with applications in R

  5. Learning R studio for R Statistical Computing.



2 comments

Recent Posts

See All
Favorite Links
Recent posts
bottom of page