频率分布表

时间:2014-04-09 00:57:52

标签: r frequency-distribution

我对[R]相对较新,我正在寻找从矢量(最有可能是数字但不总是)完成频率,相对频率,累积频率,累积相对频率来计算频率分布的最佳方法。值。下面是我提出的逻辑,但对于这样的例行任务来说似乎有点多。感谢您的反馈。

x <- c(1,2,3,2,4,2,5,4,6,7,8,9)

freq <- data.frame(table(x))

relFreq <- data.frame(prop.table(table(x)))
relFreq$Relative_Freq <- relFreq$Freq
relFreq$Freq <- NULL

Cumulative_Freq <- cumsum(table(x))

z <- cbind(merge(freq, relFreq), Cumulative_Freq)
z$Cumulative_Relative_Freq <- z$Cumulative_Freq / sum(z$Freq)

print(z)

5 个答案:

答案 0 :(得分:6)

qdap包中包含dist_tab来执行此操作:

library(qdap)
dist_tab(x)

##   interval Freq cum.Freq percent cum.percent
## 1        1    1        1    8.33        8.33
## 2        2    3        4   25.00       33.33
## 3        3    1        5    8.33       41.67
## 4        4    2        7   16.67       58.33
## 5        5    1        8    8.33       66.67
## 6        6    1        9    8.33       75.00
## 7        7    1       10    8.33       83.33
## 8        8    1       11    8.33       91.67
## 9        9    1       12    8.33      100.00

答案 1 :(得分:2)

我不知道您的确切应用,但似乎没有必要为每个重复的x值多次显示数据。如果不需要,可以避免合并

x <- c(1,2,3,2,4,2,5,4,6,7,8,9)
Freq <- table(x)
relFreq <- prop.table(Freq)
Cumulative_Freq <- cumsum(Freq)
Cumulative_Relative_Freq <- cumsum(relFreq)
data.frame(xval = names(Freq), Freq=Freq, relFreq=relFreq, 
                Cumulative_Freq=Cumulative_Freq, 
                Cumulative_Relative_Freq=Cumulative_Relative_Freq)

完成同样事情的另一种方法:

require(plyr)
x <- c(1,2,3,2,4,2,5,4,6,7,8,9)
z <- data.frame(table(x))
mutate(z, relFreq = prop.table(Freq), Cumulative_Freq = cumsum(Freq), 
               Cumulative_Relative_Freq = cumsum(relFreq))

答案 2 :(得分:0)

尝试fdth软件包:

library(fdth)

tb1 <- fdt(x) # The breaks are based on the Sturges criterion (by default)
summary(tb1)
# Class limits f   rf rf(%) cf  cf(%)
#  [0.99,2.61) 4 0.33 33.33  4  33.33
#  [2.61,4.23) 3 0.25 25.00  7  58.33
#  [4.23,5.85) 1 0.08  8.33  8  66.67
#  [5.85,7.47) 2 0.17 16.67 10  83.33
#  [7.47,9.09) 2 0.17 16.67 12 100.00

tb2 <- fdt(x, start=1, end=9, h=1)
summary(tb2)
# Class limits f   rf rf(%) cf cf(%)
#        [1,2) 1 0.08  8.33  1  8.33
#        [2,3) 3 0.25 25.00  4 33.33
#        [3,4) 1 0.08  8.33  5 41.67
#        [4,5) 2 0.17 16.67  7 58.33
#        [5,6) 1 0.08  8.33  8 66.67
#        [6,7) 1 0.08  8.33  9 75.00
#        [7,8) 1 0.08  8.33 10 83.33
#        [8,9) 1 0.08  8.33 11 91.67

答案 3 :(得分:0)

我发现此公式对于“频率分布”表很简单:

   frequency =c(9,26,11,13,3,1,2)
   # of classes
   n=7
   # of boundaries = all lower limits + one more
   table1 =data.frame(frequency distribution)
   table1
                    frequency distribution
          1                      9
          2                     26
          3                     11
          4                     13
          5                      3
          6                      1
          7                      2

答案 4 :(得分:0)

用于具有过多值的变量的频率分布 您可以折叠类中的值,

这里employrate变量的值过大,直接values_count(normalize=True)的频率分布没有意义

                country  employrate alcconsumption
0           Afghanistan   55.700001            .03
1               Albania   11.000000           7.29
2               Algeria   11.000000            .69
3               Andorra         nan          10.17
4                Angola   75.699997           5.57
..                  ...         ...            ...
208             Vietnam   71.000000           3.91
209  West Bank and Gaza   32.000000               
210         Yemen, Rep.   39.000000             .2
211              Zambia   61.000000           3.56
212            Zimbabwe   66.800003           4.96

[213 rows x 3 columns]

values_count(normalize=True)不分类的频率分布,结果长度为139(似乎无意义的频率分布):

print(gm["employrate"].value_counts(sort=False,normalize=True))

50.500000   0.005618
61.500000   0.016854
46.000000   0.011236
64.500000   0.005618
63.500000   0.005618

58.599998   0.005618
63.799999   0.011236
63.200001   0.005618
65.599998   0.005618
68.300003   0.005618
Name: employrate, Length: 139, dtype: float64

进行分类,我们将所有值都放在一定范围内。

0-10 as 1,
11-20 as 2  
21-30 as 3, and so forth.
gm["employrate"]=gm["employrate"].str.strip().dropna()  
gm["employrate"]=pd.to_numeric(gm["employrate"])
gm['employrate'] = np.where(
   (gm['employrate'] <=10) & (gm['employrate'] > 0) , 1, gm['employrate']
   )
gm['employrate'] = np.where(
   (gm['employrate'] <=20) & (gm['employrate'] > 10) , 1, gm['employrate']
   )
gm['employrate'] = np.where(
   (gm['employrate'] <=30) & (gm['employrate'] > 20) , 2, gm['employrate']
   )
gm['employrate'] = np.where(
   (gm['employrate'] <=40) & (gm['employrate'] > 30) , 3, gm['employrate']
   )
gm['employrate'] = np.where(
   (gm['employrate'] <=50) & (gm['employrate'] > 40) , 4, gm['employrate']
   )
gm['employrate'] = np.where(
   (gm['employrate'] <=60) & (gm['employrate'] > 50) , 5, gm['employrate']
   )
gm['employrate'] = np.where(
   (gm['employrate'] <=70) & (gm['employrate'] > 60) , 6, gm['employrate']
   )
gm['employrate'] = np.where(
   (gm['employrate'] <=80) & (gm['employrate'] > 70) , 7, gm['employrate']
   )
gm['employrate'] = np.where(
   (gm['employrate'] <=90) & (gm['employrate'] > 80) , 8, gm['employrate']
   )
gm['employrate'] = np.where(
   (gm['employrate'] <=100) & (gm['employrate'] > 90) , 9, gm['employrate']
   )
print(gm["employrate"].value_counts(sort=False,normalize=True))

分类后,我们有清晰的频率分布。 在这里我们可以很容易地看到,37.64%个国家/地区的雇用率在51-60%之间 和11.79%个国家/地区的雇用率在71-80%

之间
5.000000   0.376404
7.000000   0.117978
4.000000   0.179775
6.000000   0.264045
8.000000   0.033708
3.000000   0.028090
Name: employrate, dtype: float64

或者您也可以这样做,

gm.loc[(gm['employrate'] <50) & (gm['employrate'] > 40),'employrate']=4

这里的非正式语法可以是:

<dataset>.loc[<filter1> & (<filter2>),'<variable>']='<value>'