📜  如何在 R 中制作频率分布表?

📅  最后修改于: 2022-05-13 01:55:26.543000             🧑  作者: Mango

如何在 R 中制作频率分布表?

在本文中,我们将看到如何使用 R 编程语言制作频率分布表。

R 中的 table() 方法用于计算出现在数据帧指定列中的变量的频率计数。结果以两行表格结构的形式返回,其中第一行表示列的值,下一行表示其对应的频率。

frq-table <- table (x), where x is the data.table object

给定数据集的累积频率分布是得到的频率分布表中包括它下面的这个类在内的所有类的总和。任何单元格位置的值是通过将所有先前的值与迄今为止遇到的当前值相加而获得的。 cumsum()函数可用于计算此值。

cumsum( frq-table) 

相对频率也称为概率分布,是对应值的频率除以元素总数。这可以通过应用于频率表的 prop.table() 方法计算。

prop.table (frq-table)
OR
frq-table / total observations

例1:制作频率分布表。



R
library ('data.table')
 
# creating a dataframe
data_table <- data.table(col1 = sample(6 : 9, 9 ,
                                       replace = TRUE),
                          
                         col2 = letters[1 : 3],
                          
                         col3 = c(1, 4, 1, 2, 2,
                                  2, 1, 2, 2))
print ("Original DataFrame")
print (data_table)
 
freq <- table(data_table$col1)
 
print ("Modified Frequency Table")
print (freq)
 
print ("Cumulative Frequency Table")
cumsum <- cumsum(freq)
print (cumsum)
 
print ("Relative Frequency Table")
prob <- prop.table(freq)
print (prob)


R
library ('data.table')
 
# creating a dataframe
data_table <- data.table(col1 = sample(6 : 9, 9 ,
                                       replace = TRUE),
                         col2 = letters[1 : 3],
                          
                         col3 = c(1, 4, 1, 2, 2,
                                  2, 1, 2, 2))
print ("Original DataFrame")
print (data_table)
 
freq <- table(data_table$col1, data_table$col3)
print ("Modified Frequency Table")
print (freq)


R
# import libraries
library("ggplot2")
 
# creating a dataframe
data_table <- data.frame(col1 = sample(6 : 9, 9 ,
                                       replace = TRUE),
                         col2 = letters[1:3],
                         col3 = c(1, 4, 1, 2,
                                  2, 2, 1, 2, 2))
print ("Original DataFrame")
print (data_table)
 
# computing frequency
freq_tbl <- table(data_table$col1)
print ("Frequency count")
print (freq_tbl)
 
# re-order levels
frequency <- function(x) {
  factor(x, levels = names(table(x)))
}
 
# plotting the data with the help of ggplot
ggplot(data_table, aes(x = frequency(`col1`))) +
  geom_bar()


输出

[1] "Original DataFrame" 
   col1 col2 col3 
1:    9    a    1
2:    6    b    4
3:    6    c    1
4:    6    a    2
5:    8    b    2
6:    7    c    2
7:    6    a    1
8:    6    b    2
9:    8    c    2
[1] "Modified Frequency Table"
6 7 8 9 
5 1 2 1 
[1] "Cumulative Frequency Table"
6 7 8 9 
5 6 8 9 
[1] "Relative Frequency Table"
6      7      8      9 
0.5556 0.1111 0.2222 0.1111 

示例 2:

table() 方法可以接受多个参数作为其参数。所有的组合都是通过列值的交叉连接生成的,然后提取唯一元素,并计算它们对应的频率。结果以二维矩阵的形式获得,行数和列数等于所有组合,矩阵单元格值作为其计数。

电阻

library ('data.table')
 
# creating a dataframe
data_table <- data.table(col1 = sample(6 : 9, 9 ,
                                       replace = TRUE),
                         col2 = letters[1 : 3],
                          
                         col3 = c(1, 4, 1, 2, 2,
                                  2, 1, 2, 2))
print ("Original DataFrame")
print (data_table)
 
freq <- table(data_table$col1, data_table$col3)
print ("Modified Frequency Table")
print (freq)

输出

[1] "Original DataFrame"
   col1 col2 col3
1:    7    a    1
2:    7    b    4
3:    7    c    1
4:    8    a    2
5:    9    b    2
6:    9    c    2
7:    9    a    1
8:    7    b    2
9:    9    c    2 
[1] "Modified Frequency Table" 
  1 2 4   
6 2 1 0  
7 1 0 0   
8 0 1 1   
9 0 3 0

示例 3:

数据频率和累积频率表也可以通过将数据集导入工作空间来可视化。绘制频率表是为了记住数据帧的 col1。绘制了每个值的频率计数,指示其总出现次数。

电阻

# import libraries
library("ggplot2")
 
# creating a dataframe
data_table <- data.frame(col1 = sample(6 : 9, 9 ,
                                       replace = TRUE),
                         col2 = letters[1:3],
                         col3 = c(1, 4, 1, 2,
                                  2, 2, 1, 2, 2))
print ("Original DataFrame")
print (data_table)
 
# computing frequency
freq_tbl <- table(data_table$col1)
print ("Frequency count")
print (freq_tbl)
 
# re-order levels
frequency <- function(x) {
  factor(x, levels = names(table(x)))
}
 
# plotting the data with the help of ggplot
ggplot(data_table, aes(x = frequency(`col1`))) +
  geom_bar()

输出

[1] "Original DataFrame" 
col1 col2 col3 
1    6    a    1 
2    9    b    4 
3    6    c    1 
4    9    a    2 
5    8    b    2 
6    8    c    2
7    8    a    1 
8    7    b    2 
9    7    c    2