Basic Syntax: Variables in R

1198

As part of series Learn Data analytics using R lets understand basic syntax in this post.

Numeric Data Type

Lets say you want to create two variable A and B. In R program you shall assign a value to the variable using <- . Now, if you want to assign 25 to A and 29 to B. You can use the following code.

A<-25
B<-29

You can add both variable by simply using + operator

> A+B
[1] 54

Character Data Type

The above example is for assigning numeric value. If you need to assign a character/word to the variable you can do so by adding a quotations "".

A<-"Arsenal"

Now, Variable A is assigned with a text value of “Arsenal”. Character and Numeric are data types.

Logical Data Type

R is also capable of storing Logical data type like TRUE/FALSE

C<-is.numeric(B)
> C
[1] TRUE

Finding a data type

You can use class() to find the data type of a variable

class(A)
[1] "character"
> class(B)
[1] "numeric"
> class(C)
[1] "logical"

 

 

Previous articleLearn Data analytics using R
Next articleWhat is Double Irish With a Dutch Sandwich?
A.Sulthan, Ph.D.,
Author and Assistant Professor in Finance, Ardent fan of Arsenal FC. Always believe "The only good is knowledge and the only evil is ignorance - Socrates"
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments