In this article, you’ll learn about the R Object, its type and attributes.
Object in R is a data structure which is created to hold the data or value in it. R object has names, content, attributes. Everything we see or create in R is an object. They are:
R provides many functions to examine the attributes of objects :
Objects are created by assigning a value using arrow operator <- or assignment operator =.
a <- c(1,2,3) # create a object class(a) # class of object dim(a) # dimension of object length(a) # length of object
In the above code, we create a vector object using c() function which is for the vector. We will learn about a vector in another blog. The class() function gives the class of object. dim() is for the dimension of the object. The length() gives the length of the object or number of content in the object.
We will learn about the class of objects in R. we will create objects with only one value for simplicity.
Objects with string value has Character class. We can convert any objects to character values using as.character().
chadata <- 'Computer is a device.' # create object with string chadata # print the object class(chadata) # class of object
Objects with decimal value have numeric class. It is the default computational data type.
numdata <- 23.5 #create object numdata #print object class(numdata) # class of object
Integer in R is the whole number. To create Integer, we use L after the value or invoke as.integer function.
intdata <- as.integer(10) intdatum <- 20L # create objects intdata # print intdatum # print object class(intdata) class(intdatum) # class of object
A complex value is pure imaginary value i.
imgdata <- 4i # create object imgdata # print object class(imgdata) # class of object
Objects with TRUE or FALSE value is a logical class object.
x <- 1 y <- 0 logidata <- x + y # create object logidata #print object class(logidata) # class of object