1. Read file marks1.csv

df =read.csv(file="marks1.csv", head=TRUE, sep=",")

2. Check the data frame info using a few available functions

Dimmension

dim() shows the dimensions of the data frame by row and column.

dim(df)
## [1] 11 10

Tail

tail() returns the last n rows (observe the index values)

tail(df)

Summary

summary() provides summary data related to the individual object that was fed into it.

options(width=100) # for adjusting the output 
summary(df)
##        X             X.1                 test            asgn           Prsnt           Final      
##  Min.   :60001   Length:11          Min.   :12.00   Min.   :10.00   Min.   :12.00   Min.   :12.00  
##  1st Qu.:60007   Class :character   1st Qu.:17.00   1st Qu.:13.00   1st Qu.:17.00   1st Qu.:17.00  
##  Median :60011   Mode  :character   Median :21.00   Median :14.00   Median :18.00   Median :22.00  
##  Mean   :60013                      Mean   :21.45   Mean   :13.55   Mean   :17.45   Mean   :20.18  
##  3rd Qu.:60019                      3rd Qu.:25.50   3rd Qu.:15.00   3rd Qu.:19.00   3rd Qu.:23.00  
##  Max.   :60026                      Max.   :30.00   Max.   :15.00   Max.   :19.00   Max.   :28.00  
##        q1              q2              q3              q4        
##  Min.   :0.000   Min.   :3.000   Min.   :1.000   Min.   : 4.000  
##  1st Qu.:2.000   1st Qu.:5.000   1st Qu.:4.000   1st Qu.: 5.000  
##  Median :3.000   Median :5.000   Median :5.000   Median : 6.000  
##  Mean   :3.136   Mean   :5.818   Mean   :5.091   Mean   : 6.409  
##  3rd Qu.:4.000   3rd Qu.:7.000   3rd Qu.:6.500   3rd Qu.: 7.750  
##  Max.   :6.000   Max.   :9.000   Max.   :9.000   Max.   :10.000

3. Check the names of the variables in the data frame

colnames() shows the name of each column in the data frame

colnames(df)
##  [1] "X"     "X.1"   "test"  "asgn"  "Prsnt" "Final" "q1"    "q2"    "q3"    "q4"

4. Rename the first variable X to ID and 5. Rename the second variable X.1 to StuName

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.2
## -- Attaching packages ----------------------------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.5     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.1.0     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.1.2
## Warning: package 'readr' was built under R version 4.1.2
## -- Conflicts -------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
df = df %>% rename(ID = X, StuName = X.1 )
head(df)

6. Remove the first two column from the data frame

df = subset(df, select = -c(ID,StuName))
head(df)

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 = rowSums(df)
Total = apply(df,1,sum) # sum of all their marks 
print(Total)
##   1   2   3   4   5   6   7   8   9  10  11 
##  74 101 105  80  96  96 120  90  93 110  59
df <- cbind(df,Total)
head(df)

8. Using a user defined function called function(), use the apply() function to add variable 1 to variable 3 and then write to a new variable in the data frame called CW.

column_range_1=function(x,y){
  cw = apply(df[x:y],1,sum)
  return(cw)
}
#another method used
column_range_2=function(x,y){
  cw=rowSums(df[x:y], na.rm=TRUE)
  return(cw)
}

calling the function and naming the column CW

df <- cbind(df,CW=column_range_1(1,3))
df