Search code examples
rfunctiondataframefor-loopsapply

Trying to loop through columns to produce a table for each


I have a table with 10 columns. The columns have the following names: "Month", "Year", "Advising", "Hang Out", "Food", "Printing", "Supplies", "Studying", "Lending Library", "Other"

I want to loop through each of the Reason Columns and create a table broken up by month in the Y and year in the X and the sum for that Reason category.

I have attempted to create a for loop and use the sapply/lapply family of functions. However, I cannot move past "Advising." But if I run my function on each individual column it does work.

# Sample Data
AttendanceTab <- data.frame(Month = c("Oct", "Nov", "Oct", "Jan", "Feb", 
                                      "Mar", "May", "Oct"), 
                            Year = c( 2018, 2017, 2017, 2019, 2018, 2019, 
                                      2018, 2017), 
                            Advising = c(1, 1, 1, 0, 0, 1, 0, 0), 
                            Hang.Out = c(0, 0, 1, 0, 1, 1, 1, 0),
                            Food = c(1, 1, 1, 0, 0, 1, 1, 1)) 
attendance_summary <- function(ReasonName, FUN = sum) {
                    tapply(AttendanceTab[, ReasonName], 
                    AttendanceTab[,1:2], sum)
} 
attendance_summary("Advising") 
attendance_summary("Hang.Out")
attendance_summary("Food")
attendance_summary("Printing")
attendance_summary("Supplies")
attendance_summary("Studying")
attendance_summary("Lending.Library")
attendance_summary("Other") 
Complexes <- dim(AttendanceTab)[2]

reasons <- as.character(c("Advising", "Hang Out", "Food", 
          "Printing", "Supplies", "Studying", "Lending Library", 
          "Other"))
for(i in 1:Complexes) {
                    RR <- reasons[[i]]
                    ADSum <- attendance_summary(RR)
                    print(ADSum)
}
sapply(AttendanceTab, attendance_summary)
 #Also tried
sapply(reasons, attendance_summary)

for example reasons[[1]] works and gives the appropriate table, but when it moves on to the second reasons element, it returns:

Error in [.data.frame(attendance2, , ReasonName) : undefined columns selected


Solution

  • You have a typo when defining reasons. In AttendanceTab you have the column Hang.Out and in reasons you call it Hang Out - space instead of .. To avoid this, you can use

    reasons <- names(AttendanceTab)[-c(1:2)]
    Complexes <- length(reasons)