0

R for Data Science – Part 2 – Basic Syntax

Share this article!

Basic Syntax in R

This is the second part of the series “R for Data Science”. The series is as follows:

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:

[1] "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

[2] 11

> Var2 = x + (3 * y)

> Var2

[3] 29

> char<- "This is a character string"

> char

[4] "This is a character string"

 

Value of functions can also be calculated easily:

#Exponential function

> exp (6)

[5] 403.4288

#Logarithmic function

> log (70, base = 10)

[6] 1.845098

> runif(5)

[7] 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.

Installing Packages

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:

>library(package_name).

 

“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

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 😀

Share this article!

Shruti Bansal

Shruti Bansal

I am a learner and exploring the new opportunities to learn more. Currently working on data science in R. Java is my core code field and I like to solve the problems via coding. It will become easy to drive your career when your interest changes into your passion and that's what is needed.
LinkedIn : www.linkedin.com/in/shruti-bansal-16484a142
Shruti Bansal

Shruti Bansal

I am a learner and exploring the new opportunities to learn more. Currently working on data science in R. Java is my core code field and I like to solve the problems via coding. It will become easy to drive your career when your interest changes into your passion and that's what is needed. LinkedIn : www.linkedin.com/in/shruti-bansal-16484a142

Leave a Reply

Your email address will not be published. Required fields are marked *