简体   繁体   English

将数据帧列转换为R中的频率分布

[英]Convert a data frame column into a frequency distribution in R

I have recently started to work on some statistical problems in R and I have a query. 我最近开始处理R中的一些统计问题,并且有一个查询。 I normally code in python and find the "collections.Counter" function quite useful. 我通常使用python编写代码,并发现“ collections.Counter”功能非常有用。 However I did not find any such equivalent command in R which was surprising since frequencies are used a lot in statistics. 但是我没有在R中找到任何这样的等效命令,这令人惊讶,因为频率在统计中被大量使用。

for eg I have this table (data frame) - 例如,我有这张桌子(数据框)-

df ->

c1          c2
reading1    2
reading2    3
reading3    1
reading4    3
reading5    2
reading6    4
reading7    1
reading8    2
reading9    4
reading10   5 

and I want to get this in R- 我想在R-

value    frequency
    1    2
    2    3
    3    2
    4    2
    5    1

I hope this illustrates what I would like to do.. Any help is appreciated 我希望这可以说明我想做什么。

and for illustration purposes - In python I could do this - 出于说明目的-在python中,我可以这样做-

df_c2 = [2,3,1,3,2,4,1,2,4,5]
counter=collections.Counter(df$c2)
print (counter)

and get this - Counter({2: 3, 1: 2, 3: 2, 4: 2, 5: 1})
which I can manipulate using loops.

The simplest way is to use table() , which returns a named vector() : 最简单的方法是使用table() ,它返回一个命名的vector()

> table(df$c2)

1 2 3 4 5 
2 3 2 2 1 

You can return a data.frame like this: 您可以像这样返回一个data.frame

> data.frame(table(df$c2))
  Var1 Freq
1    1    2
2    2    3
3    3    2
4    4    2
5    5    1

You can, of course, also use packages like the "tidyverse". 当然,您也可以使用“ tidyverse”之类的软件包。

library(tidyverse)
df %>% 
  select(c2) %>% 
  group_by(c2) %>% 
  summarise(freq = n())
# # A tibble: 5 x 2
#      c2  freq
#   <int> <int>
# 1     1     2
# 2     2     3
# 3     3     2
# 4     4     2
# 5     5     1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM