【发布时间】:2016-03-29 10:57:34
【问题描述】:
使用以下数据集:
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
【问题讨论】: