R Programming Lab
1. Write a R program for different types of data structures in R
# R program to illustrate Vector
# Vectors(ordered collection of same data type)
X = c(1, 3, 5, 7, 8)
# Printing those elements in console
print(X)
# R program to illustrate a List
# The first attributes is a numeric vector
# containing the employee IDs which is
# created using the 'c' command here
empId = c(1, 2, 3, 4)
# The second attribute is the employee name
# which is created using this line of code here
# which is the character vector
empName = c("Debi", "Sandeep", "Subham", "Shiba")
# The third attribute is the number of employees
# which is a single numeric variable.
numberOfEmp = 4
# We can combine all these three different
# data types into a list
# containing the details of employees
# which can be done using a list command
empList = list(empId, empName, numberOfEmp)
print(empList)
# R program to illustrate dataframe
# A vector which is a character vector
Name = c("Amiya", "Raj", "Asish")
# A vector which is a character vector
Language = c("R", "Python", "Java")
# A vector which is a numeric vector
Age = c(22, 25, 45)
# To create dataframe use [Link] command
# and then pass each of the vectors
# we have created as arguments
# to the function [Link]()
df = [Link](Name, Language, Age)
print(df)
# R program to illustrate a matrix
A = matrix(
# Taking sequence of elements
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
# No of rows and columns
nrow = 3, ncol = 3,
# By default matrices are
# in column-wise order
# So this parameter decides
# how to arrange the matrix
byrow = TRUE
)
print(A)
# R program to illustrate factors
# Creating factor using factor()
fac = factor(c("Male", "Female", "Male","Male", "Female", "Male", "Female"))
print(fac)