如何从R上的表计算百分比

问题描述 投票:1回答:3

Hello,

我有一个14组的意见和16个变量表。 (S0到S11和该行的末尾的总和),我想计算总(最后一列),每个值的百分比。我试图prop.table但它不给我正确的百分比。我也尝试过申请,但同样的PB。

这里是我的表的一个示例:

Row.name    S0  S1  S2  S3  S4  S5  Total
     S0     25987   269 9152    6042    30  32  41512
     S1     234 5575    768 4398    3321    34  14330
     S2     345546  35  79  245 21685   676 368266
     S3     5678    6   78  987 4657    789 12195
     S4     9   45  879 34  5768    246 6981
     S5     54  3   788 863 56  279826  281590
     S6     367 57678   12  842 436 5824    65159 

The code I've tried : 

prop.table(df)

prop <- apply(df, 1, function(x) x/ df$Total*100)

对于例如第一线I想有(41512分之25987)* 100(41512分之269)* 100(41512分之269)* 100等

谢谢您的帮助。

r math percentage
3个回答
3
投票

尝试:

prop <- apply(df, 2,function(x,y) (x/y)*100, df$Total)

你可以从?apply读,第二个参数是:

一矢量给该函数将被施加在标。例如,对于基体1表示行,2表示列

因此,你应该使用2,而不是1,你要计算跨列百分比。此外,您的lambda函数需要一个额外的参数:它是每一行的领域总。同样,你可以从?apply读取所有功能的可选参数应该在应用的最末端。

最后,只是澄清,你还会创造,因为最后一列(总)它也是使用应用来计算的百分比最后一列是始终为1。

最好!


1
投票

prop.table()给人总的为默认的比例,但有一个保证金参数计算的行或列百分比。我认为prop.table(df[,2:7], margin = 1) * 100应该工作。其中1表示行比例来计算的(2表示列的比例)。所述2:7索引排除Total列和Row.name列作为这些都不是必需的功能。

编辑:根据类df的,可能有必要先将其转换为一个矩阵。 prop.table(as.matrix(df[,2:7]), margin = 1) * 100应该在这种情况下工作。


0
投票

您可以使用tidyverse功能gathermutateselectspread用于这一目的。

装载包和数据:

library(dplyr)
library(tidyr)
sampletable <- "Row.name    S0  S1  S2  S3  S4  S5  Total
S0     25987   269 9152    6042    30  32  41512
S1     234 5575    768 4398    3321    34  14330
S2     345546  35  79  245 21685   676 368266
S3     5678    6   78  987 4657    789 12195
S4     9   45  879 34  5768    246 6981
S5     54  3   788 863 56  279826  281590
S6     367 57678   12  842 436 5824    65159 "
dtf <- read.table(text= sampletable, header = TRUE)
# I prefer lowercase names
names(dtf) <- tolower(names(dtf))

变换长格式的数据,每行一个观察

dtflong <- dtf %>% 
    gather(col.name, value, -row.name, -total) %>% 
    mutate(percent = round(value / total *100, 2))
head(dtflong)
  row.name  total col.name  value percent
1       S0  41512       s0  25987   62.60
2       S1  14330       s0    234    1.63
3       S2 368266       s0 345546   93.83
4       S3  12195       s0   5678   46.56
5       S4   6981       s0      9    0.13
6       S5 281590       s0     54    0.02

重塑宽幅

dtflong %>% 
    select(-total, -value) %>% 
    spread(col.name, percent)

  row.name    s0    s1    s2    s3    s4    s5
1       S0 62.60  0.65 22.05 14.55  0.07  0.08
2       S1  1.63 38.90  5.36 30.69 23.18  0.24
3       S2 93.83  0.01  0.02  0.07  5.89  0.18
4       S3 46.56  0.05  0.64  8.09 38.19  6.47
5       S4  0.13  0.64 12.59  0.49 82.62  3.52
6       S5  0.02  0.00  0.28  0.31  0.02 99.37
7       S6  0.56 88.52  0.02  1.29  0.67  8.94

或者,检查总计列是正确的

dtflong %>% 
    group_by(row.name, total) %>% 
    summarise(total2 = sum(value)) %>% 
    mutate(diff = total2 - total)
# A tibble: 7 x 4
# Groups:   row.name [7]
  row.name  total total2  diff
  <fct>     <int>  <int> <int>
1 S0        41512  41512     0
2 S1        14330  14330     0
3 S2       368266 368266     0
4 S3        12195  12195     0
5 S4         6981   6981     0
6 S5       281590 281590     0
7 S6        65159  65159     0
© www.soinside.com 2019 - 2024. All rights reserved.