> For the complete documentation index, see [llms.txt](https://zpliu.gitbook.io/booknote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zpliu.gitbook.io/booknote/ggplot2/ji-suan-pin-shuai.md).

# 从数据框中计算频率

## 原始数据框

```r
V1        V2 V3
1  3 reference A2
2  6 reference A2
3  9 reference A2
4  3 reference A2
5 12 reference A2
6  1 reference A2
>
```

## 统计频率与频数

```r
#频数
table(referenceA2)
#频率
prop.table(table(referenceA2))
```

## 对一定范围内数据进行截取

将21行之后的内容进行合并

```r
colSums(tmp[(21:dim(tmp)[1]),][4]))
# 将合并后的内容整理成新的一行
c(tmp[21,][1],tmp[21,][2],tmp[21,][3],colSums(tmp[(21:dim(tmp)[1]),][4]))
#合并数据框
tmp=rbind(tmp[-(21:dim(tmp)[1]),],c(tmp[21,][1],tmp[21,][2],tmp[21,][3],colSums(tmp[(21:dim(tmp)[1]),][4])))
```

## filter函数中使用正则表达

```r
```

## 封装好的函数

```r
##对数据进行分组，并且计算每组内数据出现的频率
mergeFrequent <- function(dataframe, column) {
  library(dplyr)
  rank <- seq(0, 1, 0.2) #分成0.0 0.2 0.4 0.6 0.8 1.0 六组
  frequent <- rep(0, times = c(length(rank)))
  for (index in seq(2, length(rank), 1)) {
    frequent[index] <- dim(filter(dataframe,column > rank[index - 1] & column <= rank[index]))[1] / dim(dataframe)[1] ##计算频率
  }
  return(as.data.frame(matrix(c(rank, frequent), nrow = length(rank)))) #返回作图的数据框
}
##运行
mergeFrequent(data, data$V2)
```
