如何计算r中的列百分比

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

我在R中有一张表,想要计算每列的百分比,即X的百分比是多少?

交易< -

enter image description here

r
1个回答
0
投票

如果它已经是一个表,你只需要从下面的代码中获取底线,如果它是data.frame,你将需要转换它。

df <- data.frame(row.names = c("High", "Low", "Med"),
             A = c(0,905136,0),
             B = c(0,978531,379375),
             C = c(471235,2059469,8104087),
             D = c(244216,2873406,9842409)
             )

df <- as.table(as.matrix(df))

prop.table(df, 2)

输出以下内容:

              A          B          C          D
High 0.00000000 0.00000000 0.04431070 0.01884378
Low  1.00000000 0.72061763 0.19365392 0.22171290
Med  0.00000000 0.27938237 0.76203538 0.75944332
© www.soinside.com 2019 - 2024. All rights reserved.