将数据帧列转换为R中的频率分布 [英] Convert a data frame column into a frequency distribution in R

查看:75
本文介绍了将数据帧列转换为R中的频率分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始处理R中的一些统计问题,并且有一个查询.我通常使用python编写代码,并发现"collections.Counter"功能非常有用.但是我没有在R中找到任何这样的等效命令,这令人惊讶,因为频率在统计中被大量使用.

I have recently started to work on some statistical problems in R and I have a query. I normally code in python and find the "collections.Counter" function quite useful. However I did not find any such equivalent command in R which was surprising since frequencies are used a lot in statistics.

例如我有这张桌子(数据框)-

for e.g. 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 

我想在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

出于说明目的-在python中,我可以这样做-

and for illustration purposes - In python I could do this -

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.

推荐答案

最简单的方法是使用table(),它返回一个名为vector()

The simplest way is to use table(), which returns a named vector():

> table(df$c2)

1 2 3 4 5 
2 3 2 2 1 

您可以像这样返回data.frame:

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

当然,您也可以使用"tidyverse"之类的软件包.

You can, of course, also use packages like the "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

这篇关于将数据帧列转换为R中的频率分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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