分配因子水平权重

时间:2019-02-24 10:01:47

标签: r

我有两个因素变量,两个级别的性别和四个级别的婚姻,并且在数据框中具有相应的频率。我想使用这些频率构造一个2向列联表。有人可以帮忙吗?

 marital <- c("Single (M1)","Single (M1)", "Married (M2)","Married (M2)", 

"Vdowed (M3)","Vdowed (M3)", "Divorced (M4)", "Divorced (M4)")
    gender <- c("Male (S1)", "Female (S2)")
    freq <- c(129, 104, 298, 305, 13, 57, 40, 54)

    df <- data.frame(marital, gender, freq)
    View(df)
    table(df$marital,  df$gender)

1 个答案:

答案 0 :(得分:1)

您可以使用tidyr::spread

library(tidyr)

df %>% spread(gender, freq)

        marital Female (S2) Male (S1)
1 Divorced (M4)          54        40
2  Married (M2)         305       298
3   Single (M1)         104       129
4   Vdowed (M3)          57        13