0% found this document useful (0 votes)
14 views

Group 11 - Lab5

programming

Uploaded by

225443
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)
14 views

Group 11 - Lab5

programming

Uploaded by

225443
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/ 4

GROUP 11

#LAB 5
#
# SHARVINA UTHAYA SHANKAR 225443
#CAMELIA LEOVERA FRANCIS 225891
#SHRITHAR NAIIDU A/L RAMESH KUMAR 224327
#MUHAMMAD ADY AQMAL BIN ZAFRY 217684
#27 NOV 2024

#Question 1

(a)Read in the data from the csv file


> read.csv("fileD.csv")
> df<- read.csv("fileD.csv")
> df

(b)Get the column names


> colnames(df)
[1] "Ozone" "Solar.R" "Wind" "Temp" "Month" "Day"

(c) Extract first 2 rows


> head(x, 2)
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2

(d)Get the number of rows


> nrow(x)
[1] 153

(e)Get the value in the 47th row


> x[47,]
Ozone Solar.R Wind Temp Month Day
47 21 191 14.9 77 6 16

(f)Get the missing values in the Ozone column. How many of them?
> sum(is.na(x[,1]))
[1] 37

(g)Get the mean of the Ozone column. Exclude the missing values from the calculation
> mean(na.omit(x[,1]))
[1] 42.12931

(h)Get the mean of “Temp” when “Month” is equal to 6


> temp<- subset(x, Month==6, select = Temp)
> apply(temp, 2, mean)
Temp
79.1
#QUESTION 2

(a)Create the data frame


> y<- data.frame(name = c("Ali", "Diana", "Karim", "Jannah", "Ehsan",
"Muhammad","Maryam", "Laila", "Kasim", "Jamil"), score= c(13.5, 10.0, 17.5, 13.0, 10.0,
21.0, 15.5, 14.5, 9.0, 20.0), attempts= c(1,3,2,3,2,2,1,2,3,1), qualify= c("yes", "no", "yes",
"no", "no", "yes", "yes", "no", "no", "yes"))
> View(y)

(b)Get the dimensions of the date frame


> > dim(y)
[1] 10 4

(c)Produce the statistical summary


> summary.data.frame(y)
name score attempts qualify
Length:10 Min. : 9.00 Min. :1.00 Length:10
Class :character 1st Qu.:10.75 1st Qu.:1.25 Class :character
Mode :character Median :14.00 Median :2.00 Mode :character
Mean :14.40 Mean :2.00
3rd Qu.:17.00 3rd Qu.:2.75
Max. :21.00 Max. :3.00

(d)Extract a specific column from the data frame using the column name
> data.frame(y$name)
y.name
1 Ali
2 Diana
3 Karim
4 Jannah
5 Ehsan
6 Muhammad
7 Maryam
8 Laila
9 Kasim
10 Jamil

(e)Extract first two rows


> head(y,2)
name score attempts qualify
1 Ali 13.5 1 yes
2 Diana 10.0 3 no
(f)Extract 3rd and 5th rows with 1st and 3rd columns
> p <- y[c(3,5), c(1,3)]
>p
name attempts
3 Karim 2
5 Ehsan 2

(g)Add a new column (any attribute)


> g<- data.frame(name = c("Dev"), score= c(25.0), attempts = c(1), qualify= c("yes"),
result=c("win") )
> rbind(y,g)
name score attempts qualify result
1 Ali 13.5 1 yes win
2 Diana 10.0 3 no lose
3 Karim 17.5 2 yes win
4 Jannah 13.0 3 no lose
5 Ehsan 10.0 2 no lose
6 Muhammad 21.0 2 yes win
7 Maryam 15.5 1 yes win
8 Laila 14.5 2 no lose
9 Kasim 9.0 3 no lose
10 Jamil 20.0 1 yes win
11 Dev 25.0 1 yes win

(h)Add new rows


> k<- rbind(y,g)
> View(k)

(i)Remove/Drop the column ‘attempts’ by the column name


> k$attempts <- NULL

(j)Remove/Drop a row using the row number


> k<- k[-c(10), ]

(k)Sort the data frame by increasing order of the score


> k[order(k$score,decreasing =FALSE),]
name score qualify result
9 Kasim 9.0 no lose
2 Diana 10.0 no lose
5 Ehsan 10.0 no lose
4 Jannah 13.0 no lose
1 Ali 13.5 yes win
8 Laila 14.5 no lose
7 Maryam 15.5 yes win
3 Karim 17.5 yes win
6 Muhammad 21.0 yes win
11 Dev 25.0 yes win
(l)Change the column name ‘name’ to ‘student’

> colnames(k) <- c("student", "score", "qualify", "result")


>k
student score qualify result
1 Ali 13.5 yes win
2 Diana 10.0 no lose
3 Karim 17.5 yes win
4 Jannah 13.0 no lose
5 Ehsan 10.0 no lose
6 Muhammad 21.0 yes win
7 Maryam 15.5 yes win
8 Laila 14.5 no lose
9 Kasim 9.0 no lose
11 Dev 25.0 yes win

(m)Generate a sample of 3 students randomly from the data frame


>k
student score qualify result
1 Ali 13.5 yes win
2 Diana 10.0 no lose
3 Karim 17.5 yes win
4 Jannah 13.0 no lose
5 Ehsan 10.0 no lose
6 Muhammad 21.0 yes win
7 Maryam 15.5 yes win
8 Laila 14.5 no lose
9 Kasim 9.0 no lose
11 Dev 25.0 yes win
> print("3 samples")
[1] "3 samples"
> k[sample(which(k$student !="no"),3),]
student score qualify result
11 Dev 25.0 yes win
3 Karim 17.5 yes win
9 Kasim 9.0 no lose

(n)Save the data frame in an Rdata file


> save(k, file= "data.RData")
> load("~/week7programmimg/data.RData")

(o)Save the data frame in a csv file and open it using excel
> save(k, file = "k.csv")
> write.csv(k,file="data.csv", row.names = FALSE)

You might also like