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 -
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 - 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.
How to make it less boring (or interesting)? RStudio comes to rescue!
Download RStudio by clicking on this link
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.
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)
3. The operator "<–" (without quotes) is equivalent to "=" sign . You can use either of the operators.
4. The getwd() function shows the working directory
7. The c function is widely used to combine values to form a vector.
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.
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
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.
In R Console, you can retrieve it with the UP arrow key and edit it to run again.
21. Save data in CSV format
You can also write existing data frame in CSV format by using write.csv() function.
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.
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 -
- Exploring and Manipulating Data
- Building and validating predictive models
- Applying machine learning and text mining algorithms
- Creating visual appealing graphs
- Connecting with Databases
- Building online dynamic reports or dashboards
- Send emails or push notification via R
How to download and install R?
Download -
- To download R, visit official site of R - http://www.r-project.org/.
- Click on the link "CRAN" located at the left hand side of the page.
- Choose your country and click on the link available for your location.
- Click Download R for Windows (For Windows)
- 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 |
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.
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.
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.
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.
fix(colSums)
It opens the source code of a function. Alternatively, you can use the debug() function to view the backend code.
debug(colSums)
colSums()
19. Retrieve your previous command
20. Install and Load Packages
You can install packages by submitting the following line of code
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 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.
save.image("mydata.RData")
23. To load RData
You can use the command below to get rdata format file in R.
load("mydata.RData")
24. 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)
Excellent platform to learn R from basic.
ReplyDeleteThanks for the basics!
ReplyDeleteGreat website! thanks you..
ReplyDeleteThanks for providing valuable information.please provide more information Deepanshu.
ReplyDeleteExcellent resource for a beginner like me! Thank you!!
ReplyDeleteThank you very much for the work you do! This is perfect for beginners
ReplyDeletethank you very muck
ReplyDeleteONe of the best places to learn R .. Thank you soo much for coming up with this most useful site admin..
ReplyDeleteThanks
ReplyDeleteSuper !! Thanks
ReplyDeleteThank you very much!
ReplyDeletehow to export csv data in different drivers
ReplyDeletewrite.csv(mydata,file = "F:\\file1.csv")
What if i want to import XLS/XLSX file into R? Is there any function for that
ReplyDeleteGreat!
ReplyDeleteThanks.
Awesome very helpful.
ReplyDeleteThanks for the basics R Language !!
ReplyDeleteThank you so much for the kick start of R language
ReplyDeletewow
ReplyDelete