如何根据列获取每个值的百分比

时间:2019-10-08 21:17:31

标签: r

我正在尝试获取列中每个值的百分比。我尝试了不同的方法,但是我不知道该怎么做。

这是示例数据

This is a sample short text
It has 27 characters
gzcompress has 33 characters
gzdeflate has 27 characters
gzencode has 45 characters
bzcompress has 61 characters
base64_encode has 36 characters
bin2hex has 54 characters

这就是我要做的

第一种方法

图书馆(dplyr)

df<-structure(list(process = structure(1:16, .Label = c("A", "AA", 
"AAA", "AAAA", "AAAAA", "B", "BB", "BBB", "BBBB", "BBBBB", "BBBBBB", 
"C", "CC", "CCC", "CCCC", "CCCCC"), class = "factor"), num = c(60L, 
79L, 114L, 72L, 6L, 14L, 2L, 20L, 19L, 10L, 84L, 4L, 9L, 27L, 
5L, 10L)), class = "data.frame", row.names = c(NA, -16L))

方法2

df %>% 
  group_by( num) %>% 
  summarise( percent = 100 * n() / nrow(df))

这是计算百分比的过程 1-我应该拿总和 然后根据该总和获得价值百分比

例如,对于第一个值为60的值,它将仅占该数据的11.215%

所以输出看起来就像这些值

df %>% mutate(df, percentageNUM =num/sum(num)

0 个答案:

没有答案