Frequency count of two column in R

后端 未结 7 897
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 03:37

I have two columns in data frame

2010  1
2010  1
2010  2
2010  2
2010  3
2011  1
2011  2

I want to count frequency of both columns and get

相关标签:
7条回答
  • 2020-11-27 03:52

    If your data is dataframe df with columns y and m

    library(plyr)
    counts <- ddply(df, .(df$y, df$m), nrow)
    names(counts) <- c("y", "m", "Freq")
    
    0 讨论(0)
  • 2020-11-27 03:55

    If you had a very big data frame with many columns or didn't know the column names in advance, something like this might be useful:

    library(reshape2)
    df_counts <- melt(table(df))
    names(df_counts) <- names(df)
    colnames(df_counts)[ncol(df_counts)] <- "count"
    df_counts    
    
      y    m     count
    1 2010 1     2
    2 2011 1     1
    3 2010 2     2
    4 2011 2     1
    5 2010 3     1
    6 2011 3     0
    
    0 讨论(0)
  • 2020-11-27 03:57

    Using sqldf:

    sqldf("SELECT y, m, COUNT(*) as Freq
           FROM table1
           GROUP BY y, m")
    
    0 讨论(0)
  • 2020-11-27 04:02

    Here is a simple base R solution using table() and as.data.frame()

    df2 <- as.data.frame(table(df1))
    # df2 
         y m Freq
    1 2010 1    2
    2 2011 1    1
    3 2010 2    2
    4 2011 2    1
    5 2010 3    1
    6 2011 3    0
    
    df2[df2$Freq != 0, ]
    # output
         y m Freq
    1 2010 1    2
    2 2011 1    1
    3 2010 2    2
    4 2011 2    1
    5 2010 3    1
    

    Data

    df1 <- structure(list(y = c(2010L, 2010L, 2010L, 2010L, 2010L, 2011L, 
                               2011L), m = c(1L, 1L, 2L, 2L, 3L, 1L, 2L)), .Names = c("y", "m"
                               ), class = "data.frame", row.names = c(NA, -7L))
    
    0 讨论(0)
  • 2020-11-27 04:12

    A more idiomatic data.table version of @ugh's answer would be:

    library(data.table) # load package
    df <- data.frame(y = c(rep(2010, 5), rep(2011,2)), m = c(1,1,2,2,3,1,2)) # setup data
    dt <- data.table(df) # transpose to data.table
    dt[, list(Freq =.N), by=list(y,m)] # use list to name var directly
    
    0 讨论(0)
  • 2020-11-27 04:13
    library(data.table)
    
    oldformat <- data.table(oldformat)  ## your orignal data frame
    newformat <- oldformat[,list(Freq=length(m)), by=list(y,m)]
    
    0 讨论(0)
提交回复
热议问题