Getting Started with R


R Data Science: R Programming A-Z: R For Data Science With Real Exercises!

This tutorial is designed for beginners who are new to R language. It will help them understand the basics / fundamentals of R language. It explains how to download and install R/RStudio and also covers simple programs for practice.

What is R?

R is a free language and environment for statistical computing and graphics.

How to download and install R?

Download
  1. To download R, visit official site of R - http://www.r-project.org/
  2. Click on the link "CRAN" located at the left hand side of the page. 
  3. Choose your country and click on the link available for your location.
  4. Click Download R for Windows (For Windows)
  5. click base
Follow the steps shown in the image below.




R Window

1. To open R Editor, Go to File >> New Script (Shortcut : CTRL + N). It is where you write code or program. Press F5 to submit your code.

2. R Console - It is where you can see result/output of your code. Please note that you can write code here as well but it's difficult to edit or make changes in the R Console.
R Interface

R - Ugly Interface?

Many novice R users hate R interface. It looks boring as compared to SAS/SPSS. It is not user friendly at all. It makes writing program complex.
Bored of R Interface?

How to make it less boring (or interesting)? RStudio come to rescue!

Download RStudio by clicking on this link
R Studio Interface
What is RStudio and its benefits over standard R?

RStudio was built to make your life easy as a R programmer. It is available in open source for FREE. Unlike standard R, it supports various premium features such as intelligent code completion, syntax highlighting, structured R documentation, interactive debugging tool etc.
In layman's term, RStudio is just an interface enhancement to standard R. Programming is similar in both the platforms.

Some Useful RStudio Shortcuts

1. Press CTRL + Enter to submit code
2. Press CTRL + SHIFT + C to comment/ uncomment code
3. Press CTRL + SHIFT + N to create a new R script


Basics of R Programming

1. Write your first equation in R

Enter 5*3 in the RStudio code editor window and hit CTRL + ENTER in RStudio (or press F5 in standard R)
The [1] tells you the resulting value is the first result.

2.  The # character at the beginning of a line signifies a comment.

3.  The operator "<–" (without quotes) is equivalent to "=" sign . You can use either of the operators.

4. The getwd() function shows the working directory

5. R uses forward slashes instead of backward slashes in filenames (as shown in the image above). 

6. The setwd() function tells R where you would like your files to save (changes the working directory).
setwd ("C:/Users/Deepanshu/Downloads")
Notice the forward slash is used in the filename above.

7. The c function is widely used to combine values to form a vector.

8. In RStudio, press CTRL + SHIFT + A to format your code.

9. R uses NA to represent Not Available, or missing values.

10. To calculate sum excluding NA, use na.rm =  TRUE (By default, it is FALSE).

11. The form 1:10 generates the integers from 1 to 10.

12.  R is case-sensitive, so you have to use the exact case that the program requires.

13.  To get help for a certain function such as sum, use the form:  help (sum)

14.  Object names in R can be any length consisting of letters, numbers, underscores ‘‘_’’ or the period ‘‘.’’

15.  Object names in R should begin with a letter.

16. Unlike SAS and SPSS, R has several different data structures including vectors, factors, data frames, matrices, arrays, and lists. The data frame is most like a dataset in SAS.

17. Import CSV File to R
mydata <- read.csv("c:/mydatafile.csv", header=TRUE)
The read.csv() function is used to import CSV files in R. The header= option tells R whether column header exists in first row. In this case, dataframe would be created in R as 'mydata'

18. Open Table/ Function in R

Use fix() function

You can use fix() function and give the name of an existing function, R shows you the code for that function in a NotePad window and you can type whatever you like.

For example : fix(mydata)

When you leave NotePad, say "Yes" to the question "Do you want to save changes?" (unless you want to discard your changes). Don't "Save As...", just "Save"; R will update your function automatically.

19. Retrieve your previous command

In R Console, you can retrieve it with the UP arrow key and edit it to run again.


20. Install packages

You can install packages by submitting the following line of code
install.packages("sas7bdat")
To use the installed package, add the following line of code
library("sas7bdat") 

21. Save data in R
save.image("mywork.RData") 
22. To tell R which data set to use
attach(mydata)
If you finish with that dataset and wish to use another, you can detach it with: detach( mydata)
Coursera Data Science

R Tutorials : 75 Free R Tutorials

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

8 Responses to "Getting Started with R"

  1. Excellent platform to learn R from basic.

    ReplyDelete
  2. Thanks for the basics!

    ReplyDelete
  3. Great website! thanks you..

    ReplyDelete
  4. Thanks for providing valuable information.please provide more information Deepanshu.

    ReplyDelete
  5. Excellent resource for a beginner like me! Thank you!!

    ReplyDelete
  6. Thank you very much for the work you do! This is perfect for beginners

    ReplyDelete
  7. ONe of the best places to learn R .. Thank you soo much for coming up with this most useful site admin..

    ReplyDelete

Next → ← Prev