Data frame creation and operations
Q. (b)
(i) 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)
Answer:
# Given vectors
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)
# Create data frame and print
employee_df <- data.frame(emp_id, emp_name, start_date, salary)
print(employee_df)
Output:
emp_id emp_name start_date salary
1 1 Rick 2012-01-01 60000
2 2 Dan 2013-09-23 45000
3 3 Michelle 2014-11-15 75000
4 4 Ryan 2014-05-11 84000
5 5 Gary 2015-03-27 20000
(ii) Display structure and summary of the above data frame
# Structure
print(str(employee_df))
# Summary
print(summary(employee_df))
Structure Output:
'data.frame': 5 obs. of 4 variables:
$ emp_id : int 1 2 3 4 5
$ emp_name : chr "Rick" "Dan" "Michelle" "Ryan" "Gary"
$ start_date: chr "2012-01-01" "2013-09-23" "2014-11-15" "2014-05-11" "2015-03-27"
$ salary : num 60000 45000 75000 84000 20000
Summary Output (CORRECT “character” formatting):
emp_id emp_name start_date salary
Min. :1 Length:5 Length:5 Min. :20000
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/4
1st Qu.:2 Class :character Class :character 1st Qu.:45000
Median :3 Mode :character Mode :character Median :60000
Mean :3 Mean :56800
3rd Qu.:4 3rd Qu.:75000
Max. :5 Max. :84000
(iii) Extract the emp_name and salary columns
print(employee_df[, c("emp_name", "salary")])
Output:
emp_name salary
1 Rick 60000
2 Dan 45000
3 Michelle 75000
4 Ryan 84000
5 Gary 20000
(iv) Extract employee details whose salary is less than or equal to 60000
print(subset(employee_df, salary <= 60000))
Output:
emp_id emp_name start_date salary
1 1 Rick 2012-01-01 60000
2 2 Dan 2013-09-23 45000
5 5 Gary 2015-03-27 20000 (Give 100% same in strctured format)
Here is your 100% complete, clean, exam-ready answer with the full question + answer in
structured format, exactly how you can write it in your semester paper.
Q. (b)
(i) Create a data frame from the following 4 vectors and
demonstrate the output.
Given vectors:
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)
Create data frame and print:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/4
r
employee_df <- data.frame(emp_id, emp_name, start_date, salary)
print(employee_df)
Output:
yaml
emp_id emp_name start_date salary
1 1 Rick 2012-01-01 60000
2 2 Dan 2013-09-23 45000
3 3 Michelle 2014-11-15 75000
4 4 Ryan 2014-05-11 84000
5 5 Gary 2015-03-27 20000
(ii) Display structure and summary of the above data frame
r
str(employee_df)
summary(employee_df)
Structure Output:
ruby
'data.frame': 5 obs. of 4 variables:
$ emp_id : int 1 2 3 4 5
$ emp_name : chr "Rick" "Dan" "Michelle" "Ryan" "Gary"
$ start_date: chr "2012-01-01" "2013-09-23" "2014-11-15" "2014-05-11" "2015-03-27"
$ salary : num 60000 45000 75000 84000 20000
Summary Output (Correct character formatting):
mathematica
emp_id emp_name start_date salary
Min. :1 Length:5 Length:5 Min. :20000
1st Qu.:2 Class :character Class :character 1st Qu.:45000
Median :3 Mode :character Mode :character Median :60000
Mean :3 Mean :56800
3rd Qu.:4 3rd Qu.:75000
Max. :5 Max. :84000
(iii) Extract the emp_name and salary columns
r
employee_df[, c("emp_name", "salary")]
Output:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/4
nginx
emp_name salary
1 Rick 60000
2 Dan 45000
3 Michelle 75000
4 Ryan 84000
5 Gary 20000
(iv) Extract employee details whose salary is less than or equal to
60000
r
subset(employee_df, salary <= 60000)
Output:
yaml
emp_id emp_name start_date salary
1 1 Rick 2012-01-01 60000
2 2 Dan 2013-09-23 45000
5 5 Gary 2015-03-27 20000
If you want, I can convert this into a PDF also.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/4