# 8-4 Adding Columns and Rows

# Use the Results data from previous exercises

# The data is held in a file called Results.csv
# This is within the SP08 folder of the current working directory
# The file contains headers

# Load file

results<-read.csv("SP08/Results.csv", header=TRUE)

# Display results

results

# Adding a new column

# Create a new vector to be added to the data frame
# It must be the same length as the data frame
# The length can be calculated from any data frame column length

IDNo<-1:length(results$CW1)

# Display IDNo

IDNo

# Bind the new column to the data frame
# If you put results first, IDNo is attached at the end
# If you put IDNo first, it is attached at the front

results<-cbind(results, IDNo)

# Display results

results

# Adding a new row

# Create a new data frame to be added to the data frame
# It must have the same structure as the original data frame

newRow<-data.frame(PersonalName="Kim",
             FamilyName="Moore",
             CW1=49,
             CW2=17,
             Exam=65,
             IDNo=21)

# Bind the row to the data frame

results<-rbind(results, newRow)

# Display results

results


# Adding a calculated column

cbind(results, CW=(results$CW1+results$CW2)/2)

# Adding a calculated column in a particular position

results<-cbind(results[1:4], 
               CW=(results$CW1+results$CW2)/2, 
               results[5:6])

# Display results

results


