17

我有这个数据集:

dat <- 
structure(list(date = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 4L, 4L), .Label = c("3/31/2014", "4/1/2014", "4/2/2014", 
"4/3/2014"), class = "factor"), site = structure(c(1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("a", "b"), class = "factor"), 
clicks = c(73L, 64L, 80L, 58L, 58L, 61L, 70L, 60L, 84L, 65L, 
77L), impressions = c(55817L, 78027L, 77017L, 68797L, 92437L, 
94259L, 88418L, 55420L, 69866L, 86767L, 92088L)), .Names = c("date", 
"site", "clicks", "impressions"), class = "data.frame", row.names = c(NA, 
-11L))

dat
        date site clicks impressions
1  3/31/2014    a     73       55817
2  3/31/2014    b     64       78027
3  3/31/2014    a     80       77017
4   4/1/2014    b     58       68797
...

是否可以在链中包含一列的日期格式?(我也尝试过使用with,但只返回日期列。)

library(dplyr)

> dat %.%
+   select(date, clicks, impressions) %.%
+   group_by(date) %.%
+   summarise(clicks = sum(clicks),
+             impressions = sum(impressions)) %.%
+   as.Date(Date, format = '%m/%d/%Y')
Error in as.Date.default(`__prev`, Date, format = "%m/%d/%Y") : 
  do not know how to convert '__prev' to class “Date”

如果我不在链中包含格式,它可以工作。我知道在链外写这个很简单,但我想确认这是否可行。

dat %.%
select(date, clicks, impressions) %.%
group_by(date) %.%
summarise(clicks = sum(clicks),
          impressions = sum(impressions))

dat$date <- as.Date(dat$Date, format = '%m/%d/%Y')
4

2 回答 2

25

这是你想要的吗?

dat %>%
  select(date, clicks, impressions) %>%
  group_by(date) %>%
  summarise(clicks = sum(clicks),
            impressions = sum(impressions)) %>%
  mutate(date = as.Date(date, format = '%m/%d/%Y'))
于 2014-04-03T19:02:22.307 回答
5

有时,Error: cannot modify grouping variable当您尝试对group_by()已分组的内容运行操作时,会出现该消息。您可以ungroup先尝试包括在内。在罗伯特的答案的语法中:

dat %>%
  ungroup %>% 
  select(date, clicks, impressions) %>%
  group_by(date) %>%
  summarize(clicks      = sum(clicks),
            impressions = sum(impressions)) %>%
  mutate(date = as.Date(date, format = "%m/%d/%Y"))
于 2014-11-20T14:48:41.210 回答