尝试遍历列以为每个列生成一个表

时间:2019-06-17 23:43:26

标签: r function dataframe for-loop sapply

我有一个10列的表格。这些列具有以下名称:“月”,“年”,“建议”,“闲逛”,“食物”,“印刷”,“供应”,“学习”,“借阅图书馆”,“其他”

我想遍历每个“原因”列,并创建一个表,该表按Y(月份)和X(年份)以及该“ Reason”(原因)类别的总和分解。

我试图创建一个for循环并使用sapply / lapply系列函数。但是,我不能超越“建议”。但是,如果我在每个单独的列上运行我的函数,它将起作用。

# 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)

例如为reason [[1]]工作并给出适当的表,但是当它移至第二个原因元素时,它返回:

[.data.frame(attendance2,,ReasonName)中的错误:   未定义的列已选择

1 个答案:

答案 0 :(得分:0)

定义reasons时出现错字。在AttendanceTab中,您拥有列Hang.Out;在reasons中,您将其称为Hang Out-space而不是.。 为避免这种情况,您可以使用

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