R中两列的频率计数 [英] Frequency count of two column in R

查看:54
本文介绍了R中两列的频率计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据框中有两列

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

我想计算两列的频率并以这种格式得到结果

I want to count frequency of both columns and get the result in this format

  y    m Freq
 2010  1 2
 2010  2 2
 2010  3 1
 2011  1 1
 2011  2 1 

推荐答案

如果您的数据是数据框 df 与列 ym

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

这篇关于R中两列的频率计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆