Monday, November 6, 2023
R Programming Lab - Bsc CS DA and BSC ML And AI -Bharathiar University - Program 4- Data Frames in R
B.Sc Computer Science
R Programming - Lab
Bharathiar University
Program 4
Data Frames in R
Program 4- Write a R program to perform various operations in data frame
SOURCE CODE:
# R program for dataframe
# A vector which is a character vector
name = c("Ram", "Raja", "Rani")
# A vector which is a character vector
subject = c("R", "Python", "Java")
# A vector which is a numeric vector
mark = c(64, 72, 80)
df = data.frame(name, subject, mark)
print(df)
# Accessing first and second row
print("Accessing first and second row\n")
print(df[1:2, ])
# Accessing first and second column
print("Accessing first and second column\n")
print(df[, 1:2])
print("Before adding row\n")
print(df)
# Add a new row using rbind()
newDf = rbind(df, data.frame(name = "deepa",
subject = "C",
mark = 75
))
print("After Added a row\n")
print(newDf)
print("Before adding column\n")
print(df)
# Add a new column using cbind()
newDf = cbind(df, rank=c(3, 5, 1))
print("After Added a column\n")
print(newDf)
print("Before deleting the 3rd row and 2nd column\n")
print(df)
# delete the third row and the second column
newDF = df[-3, -2]
print("After Deleted the 3rd row and 2nd column\n")
print(newDF)
OUTPUT:
name subject mark
1 Ram R 64
2 Raja Python 72
3 Rani Java 80
[1] "Accessing first and second row\n"
name subject mark
1 Ram R 64
2 Raja Python 72
[1] "Accessing first and second column\n"
name subject
1 Ram R
2 Raja Python
3 Rani Java
[1] "Before adding row\n"
name subject mark
1 Ram R 64
2 Raja Python 72
3 Rani Java 80
[1] "After Added a row\n"
name subject mark
1 Ram R 64
2 Raja Python 72
3 Rani Java 80
4 deepa C 75
[1] "Before adding column\n"
name subject mark
1 Ram R 64
2 Raja Python 72
3 Rani Java 80
[1] "After Added a column\n"
name subject mark rank
1 Ram R 64 3
2 Raja Python 72 5
3 Rani Java 80 1
[1] "Before deleting the 3rd row and 2nd column\n"
name subject mark
1 Ram R 64
2 Raja Python 72
3 Rani Java 80
[1] "After Deleted the 3rd row and 2nd column\n"
name mark
1 Ram 64
2 Raja 72
|
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
> # Creating a factor
> ug <- factor(c("Bsc","BCom","BCA","Bsc","BCA","Bsc"))
>
> # Print the factors
> print("Factor:")
[1] "Factor:"
> print(ug)
[1] Bsc BCom BCA Bsc BCA Bsc
Levels: BCA BCom Bsc
>
>
> # Modifying a level in factor
> levels(ug)[2] <- "BBA"
>
> # Print the factors
> print("Factor after modifying level -BCOM to BBA:")
[1] "Factor after modifying level -BCOM to BBA:"
> print(ug)
[1] Bsc BBA BCA Bsc BCA Bsc
Levels: BCA BBA Bsc
>
> # Changing order of levels in factor
> ug <- factor(ug, levels = c("BBA","BCA","Bsc"))
>
> # Print the modified factors
> print("Modified levels in Factor :")
[1] "Modified levels in Factor :"
> print(ug)
[1] Bsc BBA BCA Bsc BCA Bsc
Levels: BBA BCA Bsc
>
>
> # Print elements from index 1 to 3 in factor
> print("Elements from Index 1 to 3 in Factor :")
[1] "Elements from Index 1 to 3 in Factor :"
> print(ug[1:3]) # Accessing elements from index 1 to 3 in factor
[1] Bsc BBA BCA
Levels: BBA BCA Bsc
>
> # Creating another factor
> pg <- factor(c("MCom","MCA","MSC"))
>
> # Concatenation of two factors
> college <- c(ug, pg)
> print("Concatenated Factor:")
[1] "Concatenated Factor:"
> print(college)
[1] Bsc BBA BCA Bsc BCA Bsc MCom MCA MSC
Levels: BBA BCA Bsc MCA MCom MSC
>
> # Unique values in a factor
> unique_values <- unique(ug)
> print("Unique Values in Factor:")
[1] "Unique Values in Factor:"
> print(unique_values)
[1] Bsc BBA BCA
Levels: BBA BCA Bsc
>
> # Length of factors
> length_factor <- length(ug)
> print("Length of Factor :")
[1] "Length of Factor :"
> print(length_factor)
[1] 6
>
> #Check whether it is a factor
> print("Is concatenated factor a factor?")
[1] "Is concatenated factor a factor?"
> print(is.factor(cfactor))
[1] TRUE
| |
|
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...
-
Bharathiar University Programming in C++ Lab C++ Program 1 Write a C++ Program to create a class to implement the data structure STACK. Wr...
-
Bharathiar University Programming Java Lab Java Program 2 Write a Java Program to implement the concept of multiple inheritance using Inte...
-
Bharathiar University Programming Java Lab Java Program 1 Write a Java Applications to extract a portion of a character string and print t...