#1.Read file marks1.csv
library(readr)
## Warning: 程辑包'readr'是用R版本4.1.2 来建造的
marks1 <- read.csv("C:/Users/s2124/Desktop/marks1.csv")
head(marks1)
## X X.1 test asgn Prsnt Final q1 q2 q3 q4
## 1 60001 Ahmad 15 14 17 13 0 9 2 4
## 2 60003 Abu 26 13 18 22 3 5 8 6
## 3 60006 Samy 21 15 19 25 6 7 4 8
## 4 60008 Chong 25 10 17 14 2 3 4 5
## 5 60009 Paul 25 15 16 20 3 7 6 4
## 6 60011 John 18 15 19 22 4 7 4 7
#2.Check the data frame info using a few available functions
levels(marks1$X)
## NULL
nrow(marks1)
## [1] 11
sort(marks1$X)
## [1] 60001 60003 60006 60008 60009 60011 60014 60015 60023 60025 60026
sort(marks1$X,decreasing = TRUE)
## [1] 60026 60025 60023 60015 60014 60011 60009 60008 60006 60003 60001
colnames(marks1)
## [1] "X" "X.1" "test" "asgn" "Prsnt" "Final" "q1" "q2" "q3"
## [10] "q4"
#3.Check the names of the variables in the data frame
names(marks1)
## [1] "X" "X.1" "test" "asgn" "Prsnt" "Final" "q1" "q2" "q3"
## [10] "q4"
#4.Rename the first variable X to ID
colnames(marks1)[1] <- 'ID'
#5.Rename the second variable X.1 to StuName
colnames(marks1)[2] <- 'StuName'
#6.Remove the first two column from the data frame
newtraits<-marks1[,-c(1,2)]
#7.Use apply() function to sum all the marks in the data frame and put them in a new vector called Total and bind the vector to the data frame
Total <- apply(marks1[,colnames(newtraits)], 1, sum)
marks1 <- cbind(marks1,Total)
marks1
## ID StuName test asgn Prsnt Final q1 q2 q3 q4 Total
## 1 60001 Ahmad 15 14 17 13 0.0 9 2 4.0 74
## 2 60003 Abu 26 13 18 22 3.0 5 8 6.0 101
## 3 60006 Samy 21 15 19 25 6.0 7 4 8.0 105
## 4 60008 Chong 25 10 17 14 2.0 3 4 5.0 80
## 5 60009 Paul 25 15 16 20 3.0 7 6 4.0 96
## 6 60011 John 18 15 19 22 4.0 7 4 7.0 96
## 7 60014 Devi 30 15 19 28 4.0 5 9 10.0 120
## 8 60015 Pillip 16 15 19 20 4.0 5 6 5.0 90
## 9 60023 Meilin 18 13 18 22 2.0 5 7 8.0 93
## 10 60025 Lily 30 14 18 24 5.5 6 5 7.5 110
## 11 60026 Jamil 12 10 12 12 1.0 5 1 6.0 59
#8.Using a user defined function called function(), use the apply() function to add variable 1 to variable 3, and write to a new variable in the data frame called CW.
CW <- apply(newtraits,1,function(x) {x[1]+x[2]+x[3]})
marks1 <- cbind(marks1, CW)
marks1
## ID StuName test asgn Prsnt Final q1 q2 q3 q4 Total CW
## 1 60001 Ahmad 15 14 17 13 0.0 9 2 4.0 74 46
## 2 60003 Abu 26 13 18 22 3.0 5 8 6.0 101 57
## 3 60006 Samy 21 15 19 25 6.0 7 4 8.0 105 55
## 4 60008 Chong 25 10 17 14 2.0 3 4 5.0 80 52
## 5 60009 Paul 25 15 16 20 3.0 7 6 4.0 96 56
## 6 60011 John 18 15 19 22 4.0 7 4 7.0 96 52
## 7 60014 Devi 30 15 19 28 4.0 5 9 10.0 120 64
## 8 60015 Pillip 16 15 19 20 4.0 5 6 5.0 90 50
## 9 60023 Meilin 18 13 18 22 2.0 5 7 8.0 93 49
## 10 60025 Lily 30 14 18 24 5.5 6 5 7.5 110 62
## 11 60026 Jamil 12 10 12 12 1.0 5 1 6.0 59 34
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.