创建百分比表

时间:2015-12-09 03:45:26

标签: r

我根据数据框中的两列创建了一个基于计数的表。我用过:

data.frame(table(df$State,df$Subset)[,])

有没有办法可以将这些数量转化为每个特定国家的百分比?

             S1       S2        S3       S4    S5     S6     S7
NY          195     1296       974     5528  3597    505    282
NJ          172      733       763     3253  3088    315    166
CA           48      552      1087     2073  1212   1149    203

因此,不是计数,而是每个特定州的百分比。所有纽约州的加起来都是100%,与新泽西州,加州等相同。

1 个答案:

答案 0 :(得分:3)

我不知道评论prop.table是否足够。默认情况下,tbl < structure(c(195L, 172L, 48L, 1296L, 733L, 552L, 974L, 763L, 1087L, 5528L, 3253L, 2073L, 3597L, 3088L, 1212L, 505L, 315L, 1149L, 282L, 166L, 203L), .Dim = c(3L, 7L), .Dimnames = list(c("NY", "NJ", "CA"), c("S1", "S2", "S3", "S4", "S5", "S6", "S7")), class = "table") 只会为您提供单元格比例,而请求的内容是行比例。

> prop.table(tbl)
            S1          S2          S3          S4          S5          S6
NY 0.007171491 0.047662830 0.035820676 0.203302563 0.132286418 0.018572322
NJ 0.006325622 0.026957449 0.028060755 0.119635173 0.113566989 0.011584716
CA 0.001765290 0.020300835 0.039976463 0.076238461 0.044573572 0.042256629
            S7
NY 0.010371079
NJ 0.006104961
CA 0.007465706
> prop.table(tbl, margin=1)
            S1          S2          S3          S4          S5          S6
NY 0.015755029 0.104710350 0.078694352 0.446634887 0.290619698 0.040801487
NJ 0.020259128 0.086336867 0.089870436 0.383156655 0.363722026 0.037102473
CA 0.007590133 0.087286528 0.171884883 0.327798861 0.191650854 0.181688805
            S7
NY 0.022784196
NJ 0.019552415
CA 0.032099937

比较这两个值:

> 100*prop.table(tbl, margin=1)
           S1         S2         S3         S4         S5         S6         S7
NY  1.5755029 10.4710350  7.8694352 44.6634887 29.0619698  4.0801487  2.2784196
NJ  2.0259128  8.6336867  8.9870436 38.3156655 36.3722026  3.7102473  1.9552415
CA  0.7590133  8.7286528 17.1884883 32.7798861 19.1650854 18.1688805  3.2099937

只有第二个是“百分比估计(需要乘以100)的基础:

round

我认为更有用的结果是<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>com.apple.security.app-sandbox</key> <true/> <key>com.apple.security.device.usb</key> <true/> <key>com.apple.security.files.bookmarks.app-scope</key> <true/> <key>com.apple.security.device.serial</key> <true/> <key>com.apple.security.network.client</key> <true/> <key>com.apple.security.files.user-selected.read-write</key> <true/> </dict> </plist> 将这组值设置为一个小数位。

相关问题