Basic Syntax in R
This is the second part of the series “R for Data Science”. The series is as follows:
- Part 1 – Introduction
- Part 2 – Basic Syntax (this article)
- Part 3 – Data Types and Objects
- Part 4 – Operators and Conditional Statements
- Part 5 – Loops and Control Statements
After the brief introduction to R in the first part, let us start with some basic syntax of R. You can either use RStudio or R console will also work for you. Here is the initial line of code about how to print data i.e. our first program in R.
#Print the text. >print("Welcome to R")
To run this command, you can either directly write it in console window and press enter or you can use R Script to write the command and then run.
Output will appear like this:
 "Welcome to R"
Here is some more basic syntax mentioned below with their respective outputs:
#assigning value to the variable > x <- 2 > y <- 9 > x + y  11 > Var2 = x + (3 * y) > Var2  29 > char<- "This is a character string" > char  "This is a character string"
Value of functions can also be calculated easily:
#Exponential function > exp (6)  403.4288 #Logarithmic function > log (70, base = 10)  1.845098 > runif(5)  0.65132762 0.05573036 0.07772072 0.60566683 0.51045753
The above are some examples of functions.
runif(5) generates five (or required number of ) random values between 0 and 1. We can also calculate other functions like trigonometric functions of sin(), cos(), tan() etc.
Packages are collections of well defined functions, code and data in some required format.
We can install packages according to our need and type of code we need to work on. For package installation we need an internet connection and there we go:
#for installing packages >install.packages("package_name")
We have to install package only once and then need to include its library once in a session. To include a library:
“jpeg” is one of the packages in R which is use to read, write and display bitmap images store in .jpeg format.
> install.packages("jpeg") #This is how the console appears when downloading the package. Installing package into ‘C:/Users/Documents/R/win-library/3.4’ (as ‘lib’ is unspecified) trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/jpeg_0.1-8.zip' Content type 'application/zip' length 229358 bytes (223 KB) downloaded 223 KB package ‘jpeg’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\AppData\Local\Temp\RtmpCUv1p9\downloaded_packages #After successful downloading of package, include its library to use the related code > library(jpeg)
And it’s done, now we can use any code related to “jpeg” package.
Comments are the lines ignored by the compiler and it makes the code easily readable.
In R comments are written using ‘#’ in the beginning of statement.
#This is a comment.
However, R does not support multiline comments.
When you are done with your work in R, then you can quit R session by using the following command:
# To quit the R session. >q()
Now, give it a try and we will discuss about Data Types in next tutorial. Till then, if you have any doubt, comment it out below.
Happy learning and stay tuned 😀
LinkedIn : www.linkedin.com/in/shruti-bansal-16484a142
Latest posts by Shruti Bansal (see all)
- R for Data Science – Part 5 – Loops and Control Statements - October 23, 2017
- R for Data Science – Part 2 – Basic Syntax - September 11, 2017