We have five types of objects in R to introduce for starters.
Types of objects :
1. Integer
2. Numeric
3. Character
4. Logical
5. Complex
Integer
It is a whole number like 1 , 52 ,896 ,25 etc. We create an integer object by join L with values as 1L , 2L , 10L etc. We can create integer object in this way-
a<-1L
a
Output :
[1] 1
class()
It is used to find data type of object.
class(a)
Output:
[1] “integer”
We can combine values in integer as –
x<-c(1L,2L)
x
Output:
[1] 1 2
You can check Environment window . An object x created which shows int [1:2] 1 2 . Where int means integer data type.
Errors :
When R did not find function it shows ERROR in Console window . As you can see in above snapshot.
We can also create integer object by following code :-
y<-1:5
y
You can see in Environment window here:
ls()
It shows list of objects stored in Environment window. Here , it shows objects “a” and “b”
str()
It shows structure of an object . Structure means data type and values of object.
It shows structure of object “x” .
We can also show structure of all objects stored in Environment window .
ls.str()
It shows structure of objects “a” and “x”.
rm()
It can remove object from Environment window .
rm(a)
You can check Environment window . It does not show “a” object .
We can remove all objects from window by using this command :
rm(list=ls())
It shows Environment window is empty.
Numeric
It contains both decimal and whole data . It includes values like 4 ,4.2 etc. It is default data type to store number in R.
x =12.5
x
Output:
[1] 12.5
We check data type of x by using following code :
class(x)
[1] “numeric”
We combine multiple values to store in an object as :
v<-c(4,2,5)
We check data type of “v” as –
q()
To terminate the current R session . We can quit current session using following code :
quit()
or
q()
It will open Quit R Session window as:
Save file :
You can save Script file as :
You can click on Save As…
It will open this window to save file .
To save file as “Day 1 R.R” . Enter File name as:-
File name – Day 1 R.R
Then click on Save to save file.
Character
A character object is used to represent string values in R. A character object created by enter values in quotes(”) or double quotes(“”) .
You can run following code to create character object :
You can also create character object of number as –
x<-“13.9”
x
Output :
[1] “13.9”
y<-c(“1″,”A”,”s1″)
y
Logical
It stores value in TRUE and FALSE.
We can create logical data type by using following code :
s<-TRUE
s
Output :
[1] TRUE
We also checked class of object “s” .
We can combine multiple values also. In below code , T represents TRUE and F represents FALSE
Complex
It is a combination of real and imaginary value . It is in the form of x+iy. Where “x” and “y” are real numbers and “i” shows imaginary number. You can create complex number by following code :-
Inf value
When we divide 1 by 0 . Then , it represent infinity value.
c<-1/0
c
Output :
[1] Inf
NaN value
NaN means Not A Number . The following example represent the same –
c<-0/0
c
Output :
[1] NaN