如何使用 spread() 使一列的值成为主列名称

How to make the values of one column the main column names using spread()

我需要运行卡方检验,所以我需要一列(性别)的水平作为不同变量输出的列名。这是一些数据:

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

我们可以创建一个按性别分组的序列列来做一个唯一标识符,然后使用`spread

library(dplyr)
library(tidyr)
test %>% 
    select(-test2) %>% 
    group_by(gender) %>% 
    mutate(rn = row_number()) %>% 
    spread(gender, test1)