在带有时间戳列的帧上使用group_by()进行编织

时间:2018-10-13 11:14:02

标签: r r-markdown knitr

我有一个包含案例ID和时间戳的数据框。

str(Frame$Timestamp) 
POSIXct[1:3320], format: "2018-01-02 09:10:14" "2018-01-02 09:10:14" "2018-01-02 09:35:30" "2018-01-02 10:30:43" "2018-01-02 17:10:09" ...

在控制台中,我可以毫无问题地执行group_by(Frame, CaseID)

当我使用相同的命令编织.Rmd笔记本时,出现以下错误:

Error in grouped_df_impl(data, unname(vars), drop) : 
Column 'Timestamp'is of unsupported POSIXlt/POSIXt calls:
<Anonymous> ... group_by.data.frame -> grouped_df -> grouped_df_impl Execution halted.

在这种情况下,我该怎么做才能使用group_by()

1 个答案:

答案 0 :(得分:0)

以下方法有效。

group_by()

的输出
group_by(Frame, Timestamp)
## # A tibble: 1,000 x 2
## # Groups:   Timestamp [999]
##    Timestamp           CaseID
##    <dttm>               <int>
##  1 2018-10-01 19:09:56  18592
##  2 2018-10-16 14:20:57  49269
##  3 2018-09-30 02:37:33  66986
##  4 2018-10-11 20:16:19  22090
##  5 2018-10-20 13:16:46  11802
##  6 2018-10-05 17:05:00  70791
##  7 2018-10-14 05:54:05  32192
##  8 2018-10-13 22:44:01  92938
##  9 2018-09-28 21:40:36  86432
## 10 2018-10-14 03:53:11  90539
## # ... with 990 more rows

Test.Rmd的内容

---
title: "Test"
author: "Roman Abashin"
date: "13 Oct 2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Load library 
```{r}
library(dplyr)
```

Create data
```{r}
set.seed(1701)
Frame <- data.frame(
    Timestamp = (as.POSIXct("2018-10-10 10:10:10") + 
        sample(-1000000:1000000,1000, replace = TRUE)), 
    CaseID = sample(10000:99999, 1000, replace = FALSE))
str(df)
```

Group by
```{r}
group_by(Frame, Timestamp)
```