0% found this document useful (0 votes)
14 views12 pages

R Assignment 10

Uploaded by

jitmahee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

R Assignment 10

Uploaded by

jitmahee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

TECHNO INDIA UNIVERSITY

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))

# Write the dataframe to a .CSV file


write.csv(df, file = filename, row.names = FALSE)

cat("CSV file", filename, "has been created successfully.\n")


}

# 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")
)

# Write the dataframe to a .CSV file


write.csv(df, "data.csv", row.names = FALSE)

# Read the .CSV file using scan() (row-data only)


scanned_data <- scan(file = "data.csv", what = "", sep = "\n", skip = 1)
cat("Row Data from the CSV file:\n")
print(scanned_data)
Output:-

3. Develop an R script to perform the following:


• Accepts a text-file name as input
• Accepts vectors of input one by one each containing multiple
lines of text
• Writes each of the vectors using writeLine() function
• Shows one of line data at a time until the end of file.
Code:-
# Function to accept vectors and write to a text file
write_vectors_to_file <- function(filename, ...) {
# Collect vectors as input
vectors <- list(...)

# Write vectors to file


file_conn <- file(filename, "w")
for (vector in vectors) {
writeLines(vector, file_conn)
}
close(file_conn)
# Read and display each line of the file
file_conn <- file(filename, "r")
while (length(line <- readLines(file_conn, n = 1, warn = FALSE)) > 0) {
print(line)
}
close(file_conn)
}

# 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:-

4. Develop an R script to create a text file to write the records of employees


of a company comprising EMP-ID,EMP-Name,Deptt,Dateof-Joining and
Date-Of-birth to determine and print EMP-ID,EMPName,Deptt.Years-
served and Age-on-01-01-2022 for those employ1
ees who will either complete 60 years in age or 40 years in service.Assume
that the dates are in mm-dd-yyyy format.
Code:-
# Install and load required packages
if (!requireNamespace("lubridate", quietly = TRUE)) {
install.packages("lubridate")
}
library(lubridate)
# Function to process employee data
process_employees <- function(filename) {
# Create employee data
employees <- data.frame(
EMP_ID = c(101, 102, 103),
EMP_Name = c("John Doe", "Alice Smith", "Robert Brown"),
Dept = c("HR", "IT", "Sales"),
Date_Joining = c("01-15-1982", "03-10-1990", "05-12-2000"),
Date_Birth = c("12-10-1962", "01-20-1970", "06-15-1980")
)

# Write employee data to file


write.csv(employees, filename, row.names = FALSE)

# Read and process the data


df <- read.csv(filename)
df$Years_Served <- as.integer(2022 - year(mdy(df$Date_Joining)))
df$Age_2022 <- as.integer(2022 - year(mdy(df$Date_Birth)))

# Filter employees meeting the criteria


eligible <- subset(df, Age_2022 >= 60 | Years_Served >= 40)
print(eligible[, c("EMP_ID", "EMP_Name", "Dept", "Years_Served",
"Age_2022")])
}

# Example Usage
process_employees("employee_data.csv")
Output:-

5. Develop an R script to create a text file to write the records of employees


of a company comprising EMP-ID,EMP-Name,Deptt and
Basic Pay and then to create another file containing salary data of
all the employees with the following specifications:
• Each salary record will consist of EMP-ID,EMP-Name,Deptt
,Basic Pay,DA,MA,HRA,Gross-Pay,PF,PT,IT and Net-Pay.
• DA is calculated @109% of the basic pay subject to minimum
of Rs. 10000.
• MA is calculated @21% of basic pay subject to a minimum of
Rs.3000 and a maximum of Rs.7000.
• HRA is calculated @35% of the basic pay subject to a maximum
of Rs.20,000.
• Gross-Pay is the sum of Basic Pay,DA,MA,HRA.
• PT is Rs. 100 for employees having Gross-Pay <50000, otherwise, it is Rs.
200.
• PF is 12.5% of the Gross-Pay
• IT is calculated @10% for having the Gross-Pay that exceeds
70,000 but less than 100,000;otherwise, it is 20% of the GrossPay.
• Net Pay is calculated by subtracting PF,PT and IT from the
Gross-Pay.
• Show the names of the employees in abbreviated form like Amal
Kumar Biswas as A.K. Biswas
• Format the Net-pay rounded to nearest rupees by taking the
upper value.
Code:-
# Install and load required packages
if (!requireNamespace("stringr", quietly = TRUE)) {
install.packages("stringr")
}
library(stringr)

# Function to process salary data


process_salary <- function(filename) {
# Create employee data
employees <- data.frame(
EMP_ID = c(101, 102, 103),
EMP_Name = c("John Doe", "Alice Smith", "Robert Brown"),
Dept = c("HR", "IT", "Sales"),
Basic_Pay = c(40000, 60000, 80000)
)

# Calculate salary components


employees$DA <- pmax(employees$Basic_Pay * 0.109, 10000)
employees$MA <- pmin(pmax(employees$Basic_Pay * 0.21, 3000), 7000)
employees$HRA <- pmin(employees$Basic_Pay * 0.35, 20000)
employees$Gross_Pay <- rowSums(employees[, c("Basic_Pay", "DA", "MA",
"HRA")])
employees$PF <- employees$Gross_Pay * 0.125
employees$PT <- ifelse(employees$Gross_Pay < 50000, 100, 200)
employees$IT <- ifelse(employees$Gross_Pay > 70000 &
employees$Gross_Pay < 100000,
employees$Gross_Pay * 0.1, employees$Gross_Pay * 0.2)
employees$Net_Pay <- ceiling(employees$Gross_Pay - employees$PF -
employees$PT - employees$IT)

# 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:-

6. An excel data file contains 5 sheets of data about the students of


five different classes. Develop an R script to read each of the sheets
in different .csv files to print total number of students,number of
boys,number of girls, average age of the boys and average age of
the girls for each of the classes by assuming any data structure you
deem suitable.
Code:-
# Install and load required packages
if (!requireNamespace("readxl", quietly = TRUE)) {
install.packages("readxl")
}
library(readxl)

# Function to process Excel data


process_excel_data <- function(filename) {
# Check if the file exists
if (!file.exists(filename)) {
stop(paste("Error: File", filename, "does not exist. Please check the file
path."))
}

# Try to read the file and handle any errors


tryCatch({
sheets <- excel_sheets(filename)
results <- list()

for (sheet in sheets) {


data <- read_excel(filename, sheet = sheet)

# Check if the required columns exist


if (!all(c("Gender", "Age") %in% colnames(data))) {
stop(paste("Error: Sheet", sheet, "is missing required columns 'Gender'
and/or 'Age'."))
}

# Split into boys and girls


boys <- subset(data, Gender == "Male")
girls <- subset(data, Gender == "Female")

# Collect results for each sheet


results[[sheet]] <- list(
Total_Students = nrow(data),
Boys = nrow(boys),
Girls = nrow(girls),
Avg_Age_Boys = ifelse(nrow(boys) > 0, mean(boys$Age, na.rm = TRUE),
NA),
Avg_Age_Girls = ifelse(nrow(girls) > 0, mean(girls$Age, na.rm = TRUE),
NA)
)
}

# Print the results


print(results)
}, error = function(e) {
cat("An error occurred:", e$message, "\n")
})
}

# 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:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy