R Assignment 10
R Assignment 10
SESSION: - 2023-2025
NAME: -SHASWATI PAUL
YEAR: - 2ND
BATCH: -MCA 2C
STUDENT ID: - 231001271220
SUBJECT: - R Programming Lab
ASSIGNMENT 10
Topic:Datafile Handling in R
Attempt to solve the problems given below as directed:
1. Develop a generalised R script for creating a .CSV file out of a
dataframe formed with cbind() function.
Code:-
# Generalized Script
create_csv_from_cbind <- function(data1, data2, filename) {
# Create a dataframe using cbind()
df <- as.data.frame(cbind(data1, data2))
# Example Usage
data1 <- c(1, 2, 3, 4)
data2 <- c("A", "B", "C", "D")
create_csv_from_cbind(data1, data2, "output.csv")
Output:-
2. Develop an R script to do the following:
(a) create a dataframe with any set of data of your choice
(b) write the dataframe to a .CSV file
(c) Read the .csv file using the scan() function such that only the
row-data of the dataframe are displayed with one row in one
line.
Code:-
# Create a dataframe
df <- data.frame(
Name = c("John", "Alice", "Bob"),
Age = c(25, 30, 22),
Department = c("HR", "IT", "Sales")
)
# Example Usage
vector1 <- c("Line 1 from Vector 1", "Line 2 from Vector 1")
vector2 <- c("Line 1 from Vector 2", "Line 2 from Vector 2")
write_vectors_to_file("output.txt", vector1, vector2)
Output:-
# Example Usage
process_employees("employee_data.csv")
Output:-
# Format names
employees$EMP_Name <- sapply(employees$EMP_Name, function(x) {
name_parts <- strsplit(x, " ")[[1]]
paste(paste0(substr(name_parts[-length(name_parts)], 1, 1), "."),
name_parts[length(name_parts)])
})
# Write to file
write.csv(employees, filename, row.names = FALSE)
print(employees)
}
# Example Usage
process_salary("salary_data.csv")
Output:-
# Example Usage
# Provide the correct file path to your Excel file
file_path <- "C:/Users/shasw/OneDrive/Desktop/Shaswati Paul Final
Documents/students_data.xlsx"
process_excel_data(file_path)
Output:-