比较r中样本的频率 [英] Compare frequencies of samples in r

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

问题描述

我想比较两个不同观察结果的样本频率.问题在于第一个不包含第二个数字的整个范围.我如何才能在不编写for循环的情况下将它们组合起来,而该循环则基于count返回的x值对它们进行排序? 以下是MWE的说明:

I would like to compare the frequency of samples from two different observations. The problem is that the first doesn't contain the whole range of numbers of the second. How could I combine these without writing a for loop sorting them based on the x values returned by count? Here's a MWE for clarification:

library(plyr)
a <- c(5, 4, 5, 7, 3, 5, 6, 5, 5, 4, 5, 5, 4, 5, 4, 7, 2, 4, 4, 5, 3, 6, 5, 6, 4, 4, 5, 4, 5, 5, 6, 7, 4)
b <- c(1, 3, 4, 6, 2, 7, 7, 4, 3, 6, 6, 3, 6, 6, 5, 6, 6, 5)

a.count <- count(a)
b.count <- count(b)

我想要的结果应该看起来像这样:

My desired result should look somehow like that:

   freq.a freq.b                   
1  1         
2  1       1                                       
3  3       2   
4  2      10                                              
5  2      13
6  7       4                                            
7  2       3

推荐答案

df <- merge(a.count, b.count, by ='x', all=TRUE)[2:3]
names(df) <- c('freq.a', 'freq.b')
df

  freq.a freq.b
1     NA      1
2      1      1
3      2      3
4     10      2
5     13      2
6      4      7
7      3      2

这篇关于比较r中样本的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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