jr134
jr134

Reputation: 83

How do I group my data by hour?

I have read my data into R, where I am trying to group my data by hour with the code below:

tweets <- read.csv("tweetCSV.csv", header = TRUE, sep = ",", stringsAsFactors = FALSE)

tweets %>%
group_by(format(Time, "%H"), Word) %>%
summarise(count=n())

When I run this code I get an error (shown below) which I cannot get my head around:

I was wondering if anybody can help me overcome this problem?

Thanks James

Sample of the data-set is accessible via this link: https://docs.google.com/spreadsheets/d/1JhXEyzkjPs59hVgoS3lW7e0Fcumis62QDUvuMP2q5aQ/edit?usp=sharing

Upvotes: 3

Views: 5893

Answers (1)

Adrian Martin
Adrian Martin

Reputation: 820

This worked for me:

tweets %>%
    mutate(Time = as.POSIXct(Time)) %>%
    group_by(lubridate::hour(Time), Word) %>%
    summarise(count=n()) %>%
    arrange(desc(count))

Converting it to a POSIXct lets you use lubridate's hour function which is handy.

Upvotes: 7

Related Questions