Search code examples
rapplysummary

summarizing a vector from multiple data frames


I have five data frames with a different number of vectors. Vector Q10 is present in all data frames (character; values = "Yes" or "No"). I currently check the contents of this variable this way:

table(mon1$Q10)
table(tue1$Q10)
table(wed1$Q10)
table(thr1$Q10)
table(fri1$Q10)

How can I do this more quickly? I can use a loop, but that also doesn't seem efficient. I imagine the apply functions hold the answer (I've placed my data frames into a list called svylist), but I haven't yet worked it out.

The resulting output should show counts of the "Yes" and "No" values for each data frame. A single table would be stellar, but even the output from the repeated table() functions above works fine.


Solution

  • Combine all your dataframes into a single list, and then access them using sapply:

    days1<-list(mon=mon1,tue=tue1,wed=wed1,thr=thr1,fri=fri1)
    sapply(days1, function(x) table(x$Q10))
    

    Demonstration using two dataframes coz I'm lazy:

      mon tue
    n    2   1
    y    2   3