分享

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

 geoallan 2022-12-21 发布于四川

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

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

frq-table <- table(x),其中x是data.table对象

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

cumsum(frq 表)

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

prop.table (frq-table)
要么
frq 表/总观察

例1:制作频率分布表。

  • R

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)

输出

[1] “原始数据框”
   col1 col2 col3
1: 9 1
2: 6 乙 4
3: 6 1
4: 6 一 2
5: 8 乙 2
6: 7 摄氏度 2
7: 6 一个 1
8: 6 乙 2
9: 8 摄氏度 2
[1]《修正频率表》
6 7 8 9
5 1 2 1
[1]《累积频率表》
6 7 8 9
5 6 8 9
[1]《相对频率表》
6 7 8 9
0.5556 0.1111 0.2222 0.1111

示例 2: 

The table() method can accept multiple arguments as its parameters. All the combinations are generated by the cross joining of the column values and then the unique elements are extracted, and their corresponding frequency counts too. The result is obtained in the form of 2D matrix with the number of rows and column equivalent to all combinations and matrix cell values as its counts. 

  • R

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)

Output

[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

Example 3:

The data frequency and cumulative frequency tables can also be visualized by importing the data set into the working space. The frequency table is plotted to keep col1 of the dataframe in mind. The frequency counts of each of the values are plotted indicating the number of its total occurrences. 

  • R

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

Output

[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


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多