Search code examples
rcbind

cbind for multiple table() functions


I'm trying to count the frequency of multiple columns in a data.frame.

I used the table function on each column and bound them all by cbind, and was going to use the aggregate function after to calculate the means by my identifier. Example:

df1
V1       V2     V3
George   Mary   Mary  
George   Mary   Mary
George   Mary   George
Mary     Mary   George
Mary    George  George
Mary   
Frequency<- as.data.frame(cbind(table(df1$V1), table(df1$V2), table(df1$V3)))
row.names V1
George    3
Mary      3
          1
George    1
Mary      4
          1
George    3
Mary      2

The result I get (visually) is a 2 column data frame, but when I check the dimension of Frequency, I get a result implying that the 2nd column only exists.

It's causing me trouble when I try to rename the columns and run the aggregate function, errors I get for rename:

colnames(Frequency) <- c("Name", "Frequency")
Error in names(Frequency) <- c("Name", "Frequency") : 
  'names' attribute [2] must be the same length as the vector [1]

The Final purpose is to run an aggregate command and get the mean by name:

Name.Mean<- aggregate(Frequency$Frequency, list(Frequency.Name), mean)

Desired output:

Name   Mean
George Value
Mary   Value

Solution

  • Using mtabulate (data from @user3169080's post)

    library(qdapTools)
    d1 <- mtabulate(df1)
    is.na(d1) <- d1==0 
    colMeans(d1, na.rm=TRUE)
    # Alice George   Mary 
    #  4.0    3.0    2.5