Different data types and their creation
Vector :
To slice vector :
List :
Matrices :
Arrays :
Factors :
Data frames :
SOURCE CODE :(Vector and list)
#creating character vector
days<-c(‘Sunday’,’Monday’,’Tuesday’,’Wednesday’, ‘Thursday’,’Friday’,’Saturday’)
print(class(days))
print(days)
#creating numeric vector
multi_of_tens=c(10,20,30,40,50,60,70,80,90,100)
print(class(multi_of_tens))
print(multi_of_tens)
#Vector slicing
print(multi_of_tens[1:5])
#Creating a list
list1=list(10,20L,c(‘a’,’b’,’c’),TRUE,3+2i)
print(list1)
#indexing in list
print(list1[4])
print(class(list1))
Source code :(Matrices)
#creating a matrix
m=matrix(c(1,2,3,4,5,6),ncol = 2,nrow = 3,byrow=TRUE) #filling elements row by row
n=matrix(c(1,2,3,4,5,6),ncol = 2,nrow = 3,byrow=FALSE) #filling elements column by column
cat(“Elements filled in the matrix row by row\n”)
print(m)
cat(“Elements filled in the matrix column by column\n”)
print(n)
Source code :(Arrays)
# Create an array.
a print(a)
Source code :(factors)
data=c(‘binary’,’hexadecimal’,’octal’,’decimal’,1,4,1,’binary’)
# Create a factor object.
factor_data
# Print the factor.
print(factor_data)
print(nlevels(factor_data))
Source code :(Data frames)
#creating DATA FRAMES
emp_data=data.frame(Emp_id=c(‘ID01′,’ID02′,’ID03’),Emp_name=c(‘Aks’,’Daffy’,’Sofy’),Gender=c(‘F’,’F’,’F’), Age=c(25,2,2))
print(emp_data)