You are on page 1of 6

Group 5

Section E
Reg.no.: 741 to 750

Q1. 1.CREATE A DATA FROME


emp_id emp_name emp_sal date_app
11 ram 523.90 2023-01-01
22 shyam 913.54 2023-01-02
33 mohon 641.90 2023-01-03
44 rohon 529.00 2023-01-04
55 krishna 453.25 2023-01-05

Answer – Option 1

emp.data<-data.frame(
emp_id = c(1:5),
emp_name= c("ram","shyam","mohon","rohon","krishna"),
emp_sal= c(523.90,913.54,641.90,529.0,453.25),
date_app = as.Date(c("2023-01-01","2023-01-02", "2023-01-03","2023-01-
04","2023-01-05")),
stringsAsFactors = FALSE
)
print(emp.data)

Q2. DISPLAY THE CONTENT OF THE FRAME


emp.data.emp_id emp.data.emp_name
11 ram
22 shyam
33 mohon
44 rohon
55 krishn

Answer - Option 1

f1<-data.frame(emp.data$emp_id,emp.data$emp_name)
PRINT(f1)

Q3. DISPLAY THE RECORD


emp_id emp_name emp_sal date_app
22 shyam 913.54 2023-01-02

Answer – Option 1

Q4. DISPLAY THE RECORDS AS FOLLOWS FROM THE ABOVE DATA


FRAME
emp_id emp_name emp_sal date_app
33 mohon 641.90 2023-01-03
44 rohon 529.00 2023-01-04
55 krishna 453.25 2023-01-05

Answer – Option 1

f3<-emp.data[3:5,]
PRINT(f3)

Q5. DISPLLAY THE VALUES AS FOLLOWS FROM THE ABOVE DATA


FRAME
emp_id date_app
22 2023-01-02
33 2023-01-03

Answer – Option 1

f5<-emp.data[c(2,3),c(1,4)]
PRINT(f5)

Q6. DISPLLAY THE VALUE AS FOLLOWS FROM THE ABOVE DATA


FRAME
913.54

Answer – Option 3

f7<-emp.data[c(3,3)]
PRINT(f7)
Q7. DISPLLAY THE VALUES AS FOLLOWS FROM THE ABOVE DATA
FRAME
emp_name emp_sal
ram 523.90
shyam 913.54
mohon 641.90
rohon 529.00
krishna 453.25

Answer – Option 1

f7<-emp.data[c(2,3)]
PRINT(f7)

Q8. DISPLLAY THE VALUES AS FOLLOWS AFTER ADDING THE


“ADDRESS”
emp_id emp_name emp_sal date_app Address
11 ram 523.90 2023-01-01 Delhi
22 shyam 913.54 2023-01-02 Pune
33 mohon 641.90 2023-01-03 Mumbai
44 rohon 529.00 2023-01-04 Chennai
55 krishna 453.25 2023-01-05 Kolkata

Answer – Option 1
y<-c("Delhi","Pune","Mumbai","Chennai","Kolkata")
cbind(emp.data,Address=y)
x<-list(6,"Manish",1200.00,"2023-01-06")
rbind(emp.data,x)

Q9. DRAW Multiple Lines AND display IN DIFFERENT COLORS

Answer – Option 1

line1 <- c(1,2,3,4,5,10)


line2 <- c(2,5,7,8,9,10)
plot(line1, type = "l", col = "blue")
lines(line2, type="l", col = "red")

Q10. TO DRAW SCATTER PLOT WITH TWO VECTORS

Answer – Option 1
x <- c(5,7,8,7,2,2,9,4,11,12,9,6)
y <- c(99,86,87,88,111,103,87,94,78,77,85,86)
plot(x, y, main="Observation of Cars", xlab="Car age", ylab="Car speed")

Q11. 11. TO COMPARE TWO PLOTS

Answer – Option 1

x1 <- c(5,7,8,7,2,2,9,4,11,12,9,6)
y1 <- c(99,86,87,88,111,103,87,94,78,77,85,86)
x2 <- c(2,2,8,1,15,8,12,9,7,3,11,4,7,14,12)
y2 <- c(100,105,84,105,90,99,90,95,94,100,79,112,91,80,85)
plot(x1, y1, main="Observation of Cars", xlab="Car age", ylab="Car speed",
col="red", cex=2)
points(x2, y2, col="blue", cex=2)

Q12. DRAWING A PIE CHART WITH COLORS

Answer – Option 1

x <- c(10,20,30,40)
mylabel <- c("Apples", "Bananas", "Cherries", "Dates")
pie(x, label = mylabel, main = "Fruits")
colors <- c("blue", "yellow", "green", "black")
pie(x, label = mylabel, main = "Fruits", col = colors)

Q13. DRAWING A PIE CHART WITH COLOR LABELS

Answer – Option 2

xx <- c(10,20,30,40)
mylabel <- c("Apples", "Bananas", "Cherries", "Dates")
colors <- c("blue", "yellow", "green", "black")
pie(x, label = mylabel, main = "Pie Chart", col = colors)

Q14. 14. DRAW A HORIZONTAL COMPARISON GRAPH


Answer – Option 3

xx <- c("A", "B", "C", "D")


yy <- c(2, 4, 6, 8)
barplot(y, names.arg = x, horiz = TRUE)
xx <- c(2,4,6,8,10)
yy <- c(5,7,9,11,13)
plot(x,y,main = "Values",xlab = "x values", ylab = "y values")

Q15. READ THE CSV FILE.

Answer –

# create the CSV file

data <- data.frame(id = c(11, 22, 33, 44, 55, 66, 77, 88),

name = c("Rick", "Dan", "Michelle", "Ryan", "Gary", "Nina",


"Simon", "Guru"),

salary = c(623.30, 515.20, 611.00, 729.00, 843.25, 578.00, 632.80,


722.50),

start_date = c("01-01-2012", "23-09-2013", "15-11-2014", "11-05-


2014", "27-03-2015", "21-05-2013", "30-07-2013", "17-06-2014"),

dept = c("IT", "Operations", "IT", "HR", "Finance", "IT",


"Operations", "Finance"))

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

# read the CSV file

data <- read.csv("employee_data.csv")

print(data)

Q. 16. Read MAXIMUM SALARY

Answer – Option 2

Data$ <- read.csv("input.csv")


$sal <- max(data$salary)
print(sal)

Q17. Get the person’s detail having max salary.


Answer – Option 3

$data <- read.csv("input.csv")


$sal <- max(data$salary)
retval <- subset(data, salary == max(salary))
print(retval)

Q18. ##Get all the people working in IT department

Answer – Option 1

data <- read.csv("input.csv")


retval <- subset( data, dept == "IT")
print(retval)

Q 19. ##Writing into a CSV File BY COPYING from another CSV file

Answer – Option 1

data <- read.csv("input.csv")


retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))
write.csv(retval,"output.csv")
newdata <- read.csv("output.csv")
print(newdata)

Q20.

Answer – Option 1

You might also like