Getting Started with R

Deepanshu Bhalla 18 Comments
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. R is one of the most popular programming language for data analysis and statistical modeling. Its popularity has increased many folds in recent years. It has become first choice to predictive modelers for building models as R supports thousands of packages.

What is R?

R is a free language and environment for statistical computing and graphics. You can perform a variety of tasks using R language. Some are as follows -

  1. Exploring and Manipulating Data
  2. Building and validating predictive models
  3. Applying machine learning and text mining algorithms
  4. Creating visual appealing graphs
  5. Connecting with Databases
  6. Building online dynamic reports or dashboards
  7. Send emails or push notification via R

How to download and install R?

  1. To download R, visit official site of R -
  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 comes 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)
In this example, we are asking R to open mydata data frame.

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.

Similarly, you can see the source code of any R function by using the following code.
It opens the source code of a function. Alternatively, you can use the debug() function to view the backend code.

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 and Load Packages

You can install packages by submitting the following line of code
To use the installed package, add the following line of code

21.  Save data in CSV format

You can also write existing data frame in CSV format by using write.csv() function.
write.csv(mydata, "file1.csv")
In this example, we are saving file in CSV format by name file1. You can also give full path where you want file to be saved. By default, it saves the file in the current working directory.

22. Save data in R

You can save a particular dataframe in R format.

23. To load RData

You can use the command below to get rdata format file in R.

24. To tell R which data set to use
If you finish with that dataset and wish to use another, you can detach it with: detach( mydata)
Related Posts
Spread the Word!
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 18 Responses to "Getting Started with R"
  1. Excellent platform to learn R from basic.

  2. Thanks for the basics!

  3. Great website! thanks you..

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

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

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

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

  8. how to export csv data in different drivers
    write.csv(mydata,file = "F:\\file1.csv")

  9. What if i want to import XLS/XLSX file into R? Is there any function for that

  10. Thanks for the basics R Language !!

  11. Thank you so much for the kick start of R language

Next → ← Prev