Search code examples
rfor-loopsummary

Calculate table() for all variables from a dataset in R


I want R to calculate tables for all variables in the dataset one after another. I've tried

for (variable in names(train)){
  print(paste("The table of", variable, "is: "))
  print(table(variable))
}

but it does not give tables as usual when using table(variable). I am 100% sure this is super easy but I just cannot find the way to do it since I am still a beginner. The same goes for summary().


Solution

  • Just use the *apply functions. Here is an example:

    lapply(ChickWeight, table)
    

    Same for summary.