如何用累积频率和相对频率生成R中的频率表?

时间:2021-10-12 14:55:50

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

我是r的新手,我需要生成一个简单的频率表(如书中所示),包含累积频率和相对频率。

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

a table like:

一个表:

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

5 个解决方案

#1


24  

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:

你关闭!有一些函数可以让您轻松地完成这项工作,即cumsum()和prop.table()。这就是我怎么把它们放在一起的。我做了一些随机的数据,但重点是一样的:

#Fake datax <- sample(10:20, 44, TRUE)#Your codefactorx <- factor(cut(x, breaks=nclass.Sturges(x)))#Tabulate and turn into data.framexout <- as.data.frame(table(factorx))#Add cumFreq and proportionsxout <- transform(xout, cumFreq = cumsum(Freq), relative = prop.table(Freq))#-----      factorx Freq cumFreq   relative1 (9.99,11.4]   11      11 0.250000002 (11.4,12.9]    3      14 0.068181823 (12.9,14.3]   11      25 0.250000004 (14.3,15.7]    2      27 0.045454555 (15.7,17.1]    6      33 0.136363646 (17.1,18.6]    3      36 0.068181827   (18.6,20]    8      44 0.18181818

#2


20  

The base functions table, cumsum and prop.table should get you there:

基本功能表,累加和支柱。桌子应该能让你到达那里:

 cbind( Freq=table(x), Cumul=cumsum(table(x)), relative=prop.table(table(x)))   Freq Cumul   relative10    2     2 0.0454545512    2     4 0.0454545515    1     5 0.0227272716   10    15 0.2272727317   16    31 0.3636363618    6    37 0.1363636419    4    41 0.0909090920    2    43 0.0454545522    1    44 0.02272727

With cbind and naming of the columns to your liking this should be pretty easy for you in the future. The output from the table function is a matrix, so this result is also a matrix. If this were being done on something big it would be more efficient todo this:

有了cbind和列的命名,以后对您来说应该很容易。表函数的输出是一个矩阵,所以这个结果也是一个矩阵。如果这是在大事件上进行的,那么这样做会更有效:

tbl <- table(x)cbind( Freq=tbl, Cumul=cumsum(tbl), relative=prop.table(tbl))

#3


12  

If you are looking for something pre-packaged, consider the freq() function from the descr package.

如果您正在寻找预先打包的东西,请考虑来自descr包的freq()函数。

library(descr)x = c(sample(10:20, 44, TRUE))freq(x, plot = FALSE)

Or to get cumulative percents, use the ordered() function

或者使用有序()函数来获取累积百分比

freq(ordered(x), plot = FALSE)

To add a "cumulative frequencies" column:

增加“累积频率”栏:

tab = as.data.frame(freq(ordered(x), plot = FALSE))CumFreq = cumsum(tab[-dim(tab)[1],]$Frequency)tab$CumFreq = c(CumFreq, NA)tab

If your data has missing values, a valid percent column is added to the table.

如果您的数据丢失了值,则将一个有效的百分比列添加到表中。

x = c(sample(10:20, 44, TRUE), NA, NA)freq(ordered(x), plot = FALSE)

#4


1  

Yet another possibility:

另一个可能性:

 library(SciencesPo)    x = c(sample(10:20, 50, TRUE))    freq(x)

#5


-1  

My suggestion is to check the agricolae package... check it out:

我的建议是检查一下农产品包装。检查一下:

library(agricolae)weight<-c( 68, 53, 69.5, 55, 71, 63, 76.5, 65.5, 69, 75, 76, 57, 70.5,+ 71.5, 56, 81.5, 69, 59, 67.5, 61, 68, 59.5, 56.5, 73,+ 61, 72.5, 71.5, 59.5, 74.5, 63)h1<- graph.freq(weight,col="yellow",frequency=1,las=2,xlab="h1")print(summary(h1),row.names=FALSE)

#1


24  

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:

你关闭!有一些函数可以让您轻松地完成这项工作,即cumsum()和prop.table()。这就是我怎么把它们放在一起的。我做了一些随机的数据,但重点是一样的:

#Fake datax <- sample(10:20, 44, TRUE)#Your codefactorx <- factor(cut(x, breaks=nclass.Sturges(x)))#Tabulate and turn into data.framexout <- as.data.frame(table(factorx))#Add cumFreq and proportionsxout <- transform(xout, cumFreq = cumsum(Freq), relative = prop.table(Freq))#-----      factorx Freq cumFreq   relative1 (9.99,11.4]   11      11 0.250000002 (11.4,12.9]    3      14 0.068181823 (12.9,14.3]   11      25 0.250000004 (14.3,15.7]    2      27 0.045454555 (15.7,17.1]    6      33 0.136363646 (17.1,18.6]    3      36 0.068181827   (18.6,20]    8      44 0.18181818

#2


20  

The base functions table, cumsum and prop.table should get you there:

基本功能表,累加和支柱。桌子应该能让你到达那里:

 cbind( Freq=table(x), Cumul=cumsum(table(x)), relative=prop.table(table(x)))   Freq Cumul   relative10    2     2 0.0454545512    2     4 0.0454545515    1     5 0.0227272716   10    15 0.2272727317   16    31 0.3636363618    6    37 0.1363636419    4    41 0.0909090920    2    43 0.0454545522    1    44 0.02272727

With cbind and naming of the columns to your liking this should be pretty easy for you in the future. The output from the table function is a matrix, so this result is also a matrix. If this were being done on something big it would be more efficient todo this:

有了cbind和列的命名,以后对您来说应该很容易。表函数的输出是一个矩阵,所以这个结果也是一个矩阵。如果这是在大事件上进行的,那么这样做会更有效:

tbl <- table(x)cbind( Freq=tbl, Cumul=cumsum(tbl), relative=prop.table(tbl))

#3


12  

If you are looking for something pre-packaged, consider the freq() function from the descr package.

如果您正在寻找预先打包的东西,请考虑来自descr包的freq()函数。

library(descr)x = c(sample(10:20, 44, TRUE))freq(x, plot = FALSE)

Or to get cumulative percents, use the ordered() function

或者使用有序()函数来获取累积百分比

freq(ordered(x), plot = FALSE)

To add a "cumulative frequencies" column:

增加“累积频率”栏:

tab = as.data.frame(freq(ordered(x), plot = FALSE))CumFreq = cumsum(tab[-dim(tab)[1],]$Frequency)tab$CumFreq = c(CumFreq, NA)tab

If your data has missing values, a valid percent column is added to the table.

如果您的数据丢失了值,则将一个有效的百分比列添加到表中。

x = c(sample(10:20, 44, TRUE), NA, NA)freq(ordered(x), plot = FALSE)

#4


1  

Yet another possibility:

另一个可能性:

 library(SciencesPo)    x = c(sample(10:20, 50, TRUE))    freq(x)

#5


-1  

My suggestion is to check the agricolae package... check it out:

我的建议是检查一下农产品包装。检查一下:

library(agricolae)weight<-c( 68, 53, 69.5, 55, 71, 63, 76.5, 65.5, 69, 75, 76, 57, 70.5,+ 71.5, 56, 81.5, 69, 59, 67.5, 61, 68, 59.5, 56.5, 73,+ 61, 72.5, 71.5, 59.5, 74.5, 63)h1<- graph.freq(weight,col="yellow",frequency=1,las=2,xlab="h1")print(summary(h1),row.names=FALSE)