如何在R中生成具有累积频率和相对频率的频率表 [英] How to generate a frequency table in R with with cumulative frequency and relative frequency

查看:353
本文介绍了如何在R中生成具有累积频率和相对频率的频率表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的新手.我需要生成一个具有累积频率和相对频率的简单频率表(如书籍中所示).

I'm new with R. I need to generate a simple Frequency Table (as in books) with cumulative frequency and relative frequency.

所以我想从一些简单的数据(例如

So I want to generate from some simple data like

> x
[1] 17 17 17 17 17 17 17 17 16 16 16 16 16 18 18 18 10 12 17 17 17 17 17 17 17 17 16 16 16 16 16 18 18 18 10
[36] 12 15 19 20 22 20 19 19 19

像这样的表

            frequency  cumulative   relative
(9.99,11.7]    2            2       0.04545455
(11.7,13.4]    2            4       0.04545455
(13.4,15.1]    1            5       0.02272727
(15.1,16.9]   10           15       0.22727273
(16.9,18.6]   22           37       0.50000000
(18.6,20.3]    6           43       0.13636364
(20.3,22]      1           44       0.02272727

我知道它应该很简单,但是我不知道如何.

I know it should be simple, but I don't know how.

使用此代码,我得到了一些结果:

I got some results using this code:

factorx <- factor(cut(x, breaks=nclass.Sturges(x)))
as.matrix(table(factorx))

推荐答案

您将关闭! cumsum()prop.table()这些功能可以使您轻松实现这一目标.这是我可能将其组合在一起的方式.我做了一些随机数据,但要点是一样的:

You're close! There are a few functions that will make this easy for you, namely cumsum() and prop.table(). Here's how I'd probably put this together. I make some random data, but the point is the same:

#Fake data
x <- sample(10:20, 44, TRUE)
#Your code
factorx <- factor(cut(x, breaks=nclass.Sturges(x)))
#Tabulate and turn into data.frame
xout <- as.data.frame(table(factorx))
#Add cumFreq and proportions
xout <- transform(xout, cumFreq = cumsum(Freq), relative = prop.table(Freq))
#-----
      factorx Freq cumFreq   relative
1 (9.99,11.4]   11      11 0.25000000
2 (11.4,12.9]    3      14 0.06818182
3 (12.9,14.3]   11      25 0.25000000
4 (14.3,15.7]    2      27 0.04545455
5 (15.7,17.1]    6      33 0.13636364
6 (17.1,18.6]    3      36 0.06818182
7   (18.6,20]    8      44 0.18181818

这篇关于如何在R中生成具有累积频率和相对频率的频率表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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