Saturday, November 4, 2023

R Programming Lab - Bsc CS DA and BSC ML And AI -Bharathiar University - Program 3- Operators of Factors in R

 

B.Sc Computer Science  

R Programming - Lab  

Bharathiar University 

Program 3

Operators of Factors in R

Program 3- Write a R program to create vector and perform various operations on factor

SOURCE CODE:

# Creating a factor

ug <- factor(c("Bsc","BCom","BCA","Bsc","BCA","Bsc"))


# Print the factors

print("Factor:")

print(ug)

 


# Modifying a level in factor

levels(ug)[2] <- "BBA"


# Print the factors

print("Factor after modifying level -BCOM to BBA:")

print(ug)


# Changing order of levels in factor

ug <- factor(ug, levels = c("BBA","BCA","Bsc"))


# Print the modified factors

print("Modified levels in Factor :")

print(ug)



# Print elements from index 1 to 3 in factor

print("Elements from Index 1 to 3 in Factor :")

print(ug[1:3])  # Accessing elements from index 1 to 3 in factor


# Creating another factor

pg <- factor(c("MCom","MCA","MSC"))


# Concatenation of two factors

college <- c(ug, pg)

print("Concatenated Factor:")

print(college)


# Unique values in a factor

unique_values <- unique(ug)

print("Unique Values in Factor:")

print(unique_values)


# Length of factors

l <- length(ug)

print("Length of Factor :")

print(l)


#Check whether it is a factor

print("Is concatenated factor a factor?")

print(is.factor(college))


OUTPUT



No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...