Getting Started With Learning R!!

Variable Assignment
Arithmetic Operation
Other Data Types Apart from Integers

Visit our YouTube channel for video tutorial


Basic ingredients to a good Data Science product are:

  • Maths or Statistics
  • Business i.e. knowledge about the domain you are working in
  • Technology

Assuming you understand the problem you are trying to solve and also you are aware of the techniques you would like to apply but the main question is how would you solve it? How would you scale your solution?

So basically to solve a Data Science problem you need a software tool which can talk statistics and is powerful enough.

There are multiple tools available in market today e.g., R, Python, SAS, SPSS, Excel etc. to do the job for us.

Each package has their own advantages and disadvantages. Among all of these, R and Python win the race for enthusiasts like us because it’s Free!!! Opensource to be exact

In the current work we will focus on understanding R, starting from basics and will build a platform to reach the advance R usage.


Let’s learn few basics of R Language.

  • R is a Open Source langauge, which means that anyone can contribute to the development of R. Which is the best part of R that makes it first choice of Data Scientists across the world
  • Any new Statistical Technique is first made available to R community or in other words is developed by R community (Mostly)

Before we start with the Real Stuff, please install latest version of R and R-Studio.

  • R is a case sensitive language, which means that variables in lower case and upper case have different meanings.e.g.,
    Let say, we are creating a variable ‘A’ which holds a numerical value 5
> A=5

Next, we assign value 10 to a new variable ‘a’

> a<-10

What do you think? What will be the value of ‘A’? 5 or 10?
Let’s check

> A
## [1] 5

Did you get it? I am sure you did. But is there a typing mistake in assignment? at one place we used ‘=’ and in another place place we used ‘<-‘, what is the difference?

There is not much difference between ‘=’ and ‘<-‘, R allows you to use any of these syntaxes to assign values to variables.

Interesting thing is that using ‘->’ operator you can assign value on the left to the variable on right.

> b<-6
> b
## [1] 6
> 6->b
> b
## [1] 6

Let’s do some more assignments:

> 12b<-6
## Error: <text>:1:3: unexpected symbol
## 1: 12b
##       ^
> _b<-8
## Error: <text>:1:1: unexpected input
## 1: _
##     ^
> b*c<-8
## Error in b * c <- 8: could not find function "*<-"

Both of the above assignments will give you errors, the reason being that a variable in R can not start with a number or a special character (except dot (.)).

Special characters “_” & “.” are the only two characters which can be used within a variable name.

Assigning Variables to a new variable

In R a variable can be assigned to a new or existing variable.
For example, assume we created a new variable var1

> var1<-100

And now we want to create a new variable var2 which should take value of var1

> var2<-var1
> var2
## [1] 100

Now, lets change the value of var2 to 200

> var2<-200
> var2
## [1] 200

Question for you is – What will be the value of var1 now, 100 or 200?

> var1
## [1] 100

It will be 100 only as R created a new variable named as var2 and allocated a separate memory space.

Arithmetic operation

You can use R to do basic and advance arithmetic operations. e.g.,

> 5+10
## [1] 15
> 5*10
## [1] 50
> 10-5
## [1] 5
> exp(5)
## [1] 148.4132
> 5^2 #^ symbol is used to denote power
## [1] 25
> log(5)
## [1] 1.609438
> var1*var2/10
## [1] 2000

Other data types apart from Integer

Till now we have seen presence of Integer variable in R, but R provides a lot more type of variables.

#A numeric data type
> nmrc<-5.67
> print(nmrc)
## [1] 5.67
> class(nmrc)
## [1] "numeric"
#Character or String variables
> chr<-"I"
> print(chr)
## [1] "I"
> class(chr)
## [1] "character"
> chr2<-"I Love R"
> print(chr2)
## [1] "I Love R"
> class(chr)
## [1] "character"
> chr3<-'R loves me too'
> print(chr3)
## [1] "R loves me too"
> class(chr3)
## [1] "character"
#Complex variable
> cmplx<- 5+6i
> print(cmplx)
## [1] 5+6i
> class(cmplx)
## [1] "complex"
#Logical variable
> lgcl<-TRUE
> print(lgcl)
## [1] TRUE
> class(lgcl)
## [1] "logical"

Here we have talked about Numeric, Character, Complex & Logical type of variables but there are few other types which we will talk about in later sections.

You might have noticed that we have introduced two new functions in the code above.
print() & class().
print function is used to print output to the console, You might be wondering that if we can achieve the same thing by typing variable name why do we need this function?

Let’s try to see that through an example. Let’s create a dumb function which calculates sum of two numbers. Don’t worry about the function creation and calling at this point of time

What do you think output of calling this function will be?
Will it print num3 on console?

Let’s try

> my_first_func<-function(num1,num2)
+ {
+   num3 <- num1+num2
+   num3
+ }
#Let's call the function
> summation<-my_first_func(1,2)

NO OUTPUT!!! I told you that it’s a dumb function

Let’s use print and make it smarter

> my_first_func<-function(num1,num2)
+ {
+   num3 <- num1+num2
+   print(num3)
+ }
#Let's call the function
> summation<-my_first_func(1,2)
## [1] 3

Now it is printing num3, so if you want to print something out of a function, you need to use print() function to do the same.

The next function which we introduced is class(), class() function is used to get the data type of the variable.

Visit our YouTube channel for video tutorial

Quiz Section

Let’s try your strengths in R!!!
This QUIZ provides a way to test your learning in R

The following two tabs change content below.


Leave a Reply

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