问题描述
我有一个包含 9 个协变量的模型,下面是它用来计算数据集的“是”(1) 和否(0) 响应的表之一的示例,
table(wbca1$y,wbca1$Adhes)
输出如下
我该如何编码才能获得每个协变量的样本比例,以便我有一个新表,其中有 10 列,每列代表“是”(1)?
提前致谢
解决方法
像这样:
wait(0)
或者你可以简单地做:
set.seed(111)
x = sample(1:9,100,replace=TRUE)
y = sample(0:1,replace=TRUE)
prop.table(table(y,x),margin=2)
x
y 1 2 3 4 5 6 7
0 0.4444444 0.2857143 0.6923077 0.4666667 0.5000000 0.4615385 0.6666667
1 0.5555556 0.7142857 0.3076923 0.5333333 0.5000000 0.5384615 0.3333333
x
y 8 9
0 0.3636364 0.4615385
1 0.6363636 0.5384615
,
使用 tidyverse
library(dplyr)
tibble(x,y) %>%
count(x,y) %>%
mutate(prop = n/sum(n))