【发布时间】:2020-04-19 20:36:03
【问题描述】:
我需要运行卡方检验,因此我需要一列(性别)的级别作为不同变量输出的列名。以下是一些数据:
test <- data.frame(gender = as.character(sample(c('male','female'),10, replace = T)),
test1 = sample(c(1:10)),
test2 = sample(1:5,10 , replace = T))
> test
gender test1 test2
1 female 2 2
2 male 9 1
3 male 4 4
4 female 8 1
5 female 5 4
6 female 3 3
7 female 7 3
8 female 1 1
9 male 10 2
10 male 6 2
我在 dplyr::spread() 中使用了以下代码行,但它给了我一个错误:
test %>% spread(gender,test1)
Error: Each row of output must be identified by a unique combination of keys.
我已经使用了 dplyr 提供的所有示例,使用 gather() 和 spread() 但没有任何效果。如果您有任何提示,请告诉我。这是我想要的结果:
> goal
male female
1 10 3
2 1 4
3 5 10
4 3 9
5 6 7
【问题讨论】:
-
你的例子。显示 test1 有女性 1 而不是男性 1