0% found this document useful (0 votes)
168 views7 pages

R Programming Code

The document contains R programming scripts for various tasks including sorting vectors, creating data frames, combining datasets, and subsetting data. It demonstrates how to manipulate and analyze data using R functions and methods. Each question provides specific instructions and code examples for the tasks to be performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views7 pages

R Programming Code

The document contains R programming scripts for various tasks including sorting vectors, creating data frames, combining datasets, and subsetting data. It demonstrates how to manipulate and analyze data using R functions and methods. Each question provides specific instructions and code examples for the tasks to be performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

R Programming Code

Q1) Write the script to sort the values contained in the following vector in ascending order
and descending order (46,23,15,38,98,56,28,78)
# Define the vector
v <- c(46, 23, 15, 38, 98, 56, 28, 78)
# Sort in ascending order
asc <- sort(v)
print("Ascending Order:")
print(asc)
# Sort in descending order
desc <- sort(v, decreasing = TRUE)
print("Descending Order:")
print(desc)

Q2) Write a script to create a dataset named data1 in R containing the following text
text: 2,3,4,5,6.7,7,8.1,9
# Create a dataset named data1 with the given text values
data1 <- data.frame(
text = c(2, 3, 4, 5, 6.7, 7, 8.1, 9)
)
# Print the dataset
print(data1)

Q3) Suppose you have two subsets A and B


Dataset A has the following data : 6 7 8 9
Dataset A has the following data : 1 2 4 5
Which function is used to combine the data from both datasets into dataset C
Demonstrate the function with the input values and write the output

# Dataset A
A <- c(6, 7, 8, 9)
# Dataset B
B <- c(1, 2, 4, 5)
# Combine A and B into C
C <- c(A, B)
# Print all datasets
cat("Dataset A:", A, "\n")
cat("Dataset B:", B, "\n")
cat("Dataset C (Combined):", C, "\n")
Q4) a) Create a data frame from the following 4 vectors and demonstrate the output:
emp_id = c(1:5)
emp_name = c("Rick","Dan", "Michelle", "Ryan","Gary")
start_date = c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11", "2015-03-27")
salary = c(60000, 45000, 75000, 84000, 20000)
b) Display structure and summary of the above data frame.
c) Extract the emp_name and salary columns from the above data frame.
d) Extract the employee details whose salary is less than or equal to 60000.

# a) Create a data frame


employee <- data.frame(
emp_id = c(1:5),
emp_name = c("Rick", "Dan", "Michelle", "Ryan",
"Gary"),
start_date = as.Date(c("2012-01-01", "2013-09-23",
"2014-11-15", "2014-05-11", "2015-03-27")),
salary = c(60000, 45000, 75000, 84000, 20000)
)
# Display the data frame
print("Employee Data Frame:")
print(employee)
# b) Display structure and summary of the data frame
print("Structure of Data Frame:")
str(employee)
print("Summary of Data Frame:")
summary(employee)
# c) Extract emp_name and salary columns
print("Extract emp_name and salary:")
print(employee[, c("emp_name", "salary")])
# d) Extract employee details whose salary <= 60000
print("Employees with salary <= 60000:")
print(employee[employee$salary <= 60000, ])

Q5)
# Create numeric vectors for each product
bread <- c(12, 3, 5, 11, 9)
milk <- c(21, 27, 18, 20, 15)
cola_cans <- c(10, 1, 33, 6, 12)
chocolate_bars <- c(6, 7, 4, 13, 12)
detergent <- c(5, 8, 12, 20, 23)
# Print the vectors
print("Bread sales:"); print(bread)
print("Milk sales:"); print(milk)
print("Cola Cans sales:"); print(cola_cans)
print("Chocolate bars sales:");
print(chocolate_bars)
print("Detergent sales:"); print(detergent)

Q6)

# Create the data frame


course <- c(1, 2, 3, 4, 5, 6)
id <- c(11, 12, 13, 14, 15, 16)
class <- c(1, 2, 1, 2, 1, 2)
marks <- c(56, 75, 48, 69, 84, 53)
df <- data.frame(course, id, class, marks)
print("Original Data Frame:")
print(df)
# i. Subset of course < 3 using [] brackets
print("Subset where course < 3:")
print(df[df$course < 3, ])
# ii. Subset where course < 3 OR class = 2 using
subset()
print("Subset where course < 3 OR class = 2:")
print(subset(df, course < 3 | class == 2))
Q7) Write the script to sort the values contained in the following vector in ascending order
and descending order (23,45,10,34,89,20,67,99)

# Define the vector


v <- c(23,45,10,34,89,20,67,99)
# Sort in ascending order
asc <- sort(v)
print("Ascending Order:")
print(asc)
# Sort in descending order
desc <- sort(v, decreasing = TRUE)
print("Descending Order:")
print(desc)
Q8)

# i) Create Data Frame for 10 employees


sr_no <- 1:10
name <- c("Vivek", "Karan", "James", "Soham", "Renu",
"Farah", "Hetal", "Mary", "Ganesh", "Krish")
salary <- c(21000, 55000, 67000, 50000, 54000,
40000, 30000, 70000, 20000, 15000)
employees <- data.frame(Sr_No = sr_no, Name = name, Salary = salary)
print("Original Employee Data Frame:")
print(employees)
# ii) Add 5 new employees using rbind()
new_sr_no <- 11:15
new_name <- c("Ajay", "Neha", "Rahul", "Pooja", "Simran")
new_salary <- c(60000, 45000, 72000, 38000, 51000)
new_employees <- data.frame(Sr_No = new_sr_no, Name = new_name, Salary =
new_salary)
# Join datasets
updated_employees <- rbind(employees, new_employees)
print("Updated Employee Data Frame with New Employees:")
print(updated_employees)

Q9)

# Create the data frame


subject <- c(1, 2, 3, 4, 5, 6)
class <- c(1, 2, 1, 2, 1, 2)
marks <- c(56, 75, 48, 69, 84, 53)
df <- data.frame(subject, class, marks)
print("Original Data Frame:")
print(df)
# i) Subset of subject < 4 using subset()
subset1 <- subset(df, subject < 4)
print("Subset where subject < 4:")
print(subset1)
# ii) Subset where subject < 3 and class = 2 using []
subset2 <- df[df$subject < 3 & df$class == 2, ]
print("Subset where subject < 3 and class = 2:")
print(subset2)
Q10)

# i) Create initial data frame


ProjectId <- c(1, 2, 3, 4, 5)
ProjectName <- c("CRM Implementation", "Cloud
Infrastructure","Network Upgrade", "E-Commerce
Platform", "Data Analytics")
Budget <- c(120000, 180000, 60000, 220000, 90000)
Status <- c("In Progress", "Completed", "Not Started",
"Completed", "In Progress")
projects <- data.frame(ProjectId, ProjectName, Budget,
Status)
# Display the data frame
print("Initial Project Data Frame:")
print(projects)
# ii) Add new projects
new_ProjectId <- c(6, 7)
new_ProjectName <- c("UX Research", "Cloud Integration")
new_Budget <- c(160000, 190000)
new_Status <- c("Not Started", "Not Started")
new_projects <- data.frame(ProjectId = new_ProjectId, ProjectName = new_ProjectName,
Budget = new_Budget, Status = new_Status)
# Update the data frame by adding new rows
projects <- rbind(projects, new_projects)
# Display the updated data frame
print("Updated Project Data Frame:")
print(projects)
Q11)

# i) Create a Data Frame for the project data


ProjectId <- c(1, 2, 3, 4, 5)
ProjectName <- c("Website Redesign", "Mobile App Launch", "Data Migration", "AI
Development", "Cybersecurity Audit")
Budget <- c(150000, 100000, 80000, 200000, 50000)
project_data <- data.frame(ProjectId, ProjectName, Budget)
# Display the Data Frame
print("Project Data Frame:")
print(project_data)
# ii) Show structure and summary statistics
print("Structure of Data Frame:")
str(project_data)
print("Summary of Data Frame:")
summary(project_data)

You might also like