Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从R上的表格计算百分比_R_Math_Percentage - Fatal编程技术网

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

如何从R上的表格计算百分比,r,math,percentage,R,Math,Percentage,我有一个包含14个观察值和16个变量的表。(S0到S11以及最后一行的总和) 我想计算每个值占总数的百分比(最后一列)。 我试过道具台,但它没有给我正确的百分比。我也试着申请了,但还是一样的pb 以下是我的表格示例: Row.name S0 S1 S2 S3 S4 S5 Total S0 25987 269 9152 6042 30 32 41512 S1 234 5575 768 4398 3321 3

我有一个包含14个观察值和16个变量的表。(S0到S11以及最后一行的总和) 我想计算每个值占总数的百分比(最后一列)。 我试过道具台,但它没有给我正确的百分比。我也试着申请了,但还是一样的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)
行名称S0 S1 S2 S3 S4 S5总计
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
我尝试过的代码:
道具台(df)
试试看:

prop试试:

prop
prop.table()
默认给出了总计的比例,但有一个边距参数来计算行或列的百分比。我认为
prop.table(df[,2:7],margin=1)*100应该可以用。其中1表示要计算行比例(2表示列比例)。
2:7
索引不包括
Total
列和
Row.name
列,因为这两个列不是函数所必需的

编辑:根据df的类别,可能需要先将其转换为矩阵
prop.table(如.matrix(df[,2:7]),margin=1)*100在这种情况下应该可以使用。

prop.table()
默认给出了总计的比例,但有一个margin参数来计算行或列的百分比。我认为
prop.table(df[,2:7],margin=1)*100应该可以用。其中1表示要计算行比例(2表示列比例)。
2:7
索引不包括
Total
列和
Row.name
列,因为这两个列不是函数所必需的

编辑:根据df的类别,可能需要先将其转换为矩阵<代码>属性表(如矩阵(df[,2:7]),边距=1)*100在这种情况下应该可以工作。

您可以使用这些函数 , , 为此目的

加载包和数据:

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 %>% 
    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
dtflong%>%
分组依据(行名称,总计)%>%
汇总(总计2=总和(值))%>%
变异(差异=总计2-总计)
#一个tibble:7x4
#组:row.name[7]
row.name总计2个差异
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
7 S6 65159 651590
您可以使用这些功能 , , 为此目的

加载包和数据:

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 %>% 
    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
dtflong%>%
分组依据(行名称,总计)%>%
汇总(总计2=总和(值))%>%
变异(差异=总计2-总计)
#一个tibble:7x4
#组:row.name[7]
row.name总计2个差异
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
7 S6 65159 651590

感谢您的反馈,@DebanjanB感谢您的反馈,@DebanjanB