在 R tidyr 的 spread_ 上使用 NSE

using NSE on spread_ in R tidyr

使用以下数据集:

temp <- structure(list(
  GENDER = structure(c(1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L), 
                     .Label = c("F", "M"), 
                     class = "factor"), 
  EVERFSM_6 = c(0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L), 
  `0001` = c(0, 11, 22, 33, 33, 55, 66, 77, 88, 0), 
  n = c(20L, 13L, 4L, 13L, 36L, 94L, 28L, 50L, 27L, 1L)), 
  .Names = c("GENDER", "EVERFSM_6", "0001", "n"), 
  class = c("tbl_df", "data.frame"), 
  row.names = c(NA, -10L))

我正在尝试执行以下 spread_ 操作来汇总数据:

DiscID <- "0001"
colID <- as.name(DiscID)
cols <- c("GENDER", colID, "n")

gender_results <- temp %>%
  select_(.dots=cols) %>%
  group_by_(.dots=cols[1:2]) %>%
  summarise(gender_n = sum(n)) %>% 
  spread_(paste0("`",DiscID,"`"), "gender_n") %>%
  rename(type = GENDER)

但是它说:

Error: Key column '`0001`' does not exist in input.

我必须使用 _ 版本的 select_、group_by_ 和 spread_,因为我正在使用变量来引用列名。所需的输出如下,可通过使用硬编码实现:

spread(`0001`, gender_n) %>%

    type     0    11    22    33    55    66    77    88
  (fctr) (int) (int) (int) (int) (int) (int) (int) (int)
1      F    20    13     4    36    94    28    NA    NA
2      M     1    NA    NA    13    NA    NA    50    27

我认为你的 DiscID 已经可以与 spread_ 一起使用,不需要 paste。这有帮助吗?

cols <- c("GENDER", colID, "n")
gender_results <- temp %>%
  select_(.dots=cols) %>%
  group_by_(.dots=cols[1:2]) %>%
  summarise(gender_n = sum(n)) %>% 
  spread_(DiscID, "gender_n") %>%
  rename(type = GENDER)

或者:

cols <- list(~GENDER, colID, ~n)
gender_results <- temp %>%
  select_(.dots=cols) %>%
  group_by_(.dots=cols[1:2]) %>%
  summarise(gender_n = sum(n)) %>% 
  spread_(DiscID, "gender_n") %>%
  rename(type = GENDER)

来自NSE vignette in dplyr