0

The timestamp data is in chr as follows:

posted_at: chr  "2012-01-29 19:48:33" "2012-02-02 15:53:13" "2012-10-24 17:11:40" "2014-07-12 17:00:00" ...
   [1] "2012-01-29 19:48:33" "2012-02-02 15:53:13" "2012-10-24 17:11:40" "2014-07-12 17:00:00" "2014-07-31 08:08:31"
   [6] "2014-07-31 10:48:25" "2014-08-06 09:24:38" "2015-06-16 15:55:28" "2015-06-16 19:56:28" "2015-06-25 17:20:29"
  [11] "2015-06-26 18:28:31" 

I tried converting it using strptime() :

tweet_text$posted_at <- strptime(trump_text$posted_at, "%Y-%m-%d %H:%M:%S")

It gets converted as follows:

posted_at: POSIXlt, format: "2012-01-29 19:48:33" "2012-02-02 15:53:13" "2012-10-24 17:11:40" "2014-07-12 17:00:00" ...
   [1] "2012-01-29 19:48:33 AEDT" "2012-02-02 15:53:13 AEDT" "2012-10-24 17:11:40 AEDT" "2014-07-12 17:00:00 AEST"
   [5] "2014-07-31 08:08:31 AEST" "2014-07-31 10:48:25 AEST" "2014-08-06 09:24:38 AEST" "2015-06-16 15:55:28 AEST"
   [9] "2015-06-16 19:56:28 AEST" "2015-06-25 17:20:29 AEST" "2015-06-26 18:28:31 AEST" "2015-07-01 15:58:41 AEST"
  [13] "2015-07-01 18:05:15 AEST

How do I use hist() to plot this timestamp data based on years and count and other different combinations .

1
  • You can use barplot(table(format(trump_text$posted_at, '%Y'))) Commented Oct 14, 2020 at 1:31

2 Answers 2

1

We can take the frequency count with table on the extracted 'Year' from the Datetime object and do a barplot or hist. No external packages used

hist(as.numeric(format(trump_text$posted_at, '%Y')))

Using a reproducible exampe

v1 <- as.POSIXlt(sample(seq(Sys.time(), length.out = 20, by = 'year'), 200, replace = TRUE))
hist(as.numeric(format(v1, '%Y')))

-output enter image description here


Or another option is table with barplot

barplot(table(format(v1, '%Y')))

Or using tidyverse

library(dplyr)
library(lubridate)
library(ggplot2)
tibble(v1 = v1) %>%
     mutate(year = year(v1)) %>%
     ggplot(aes(year)) +
     geom_histogram(stat = 'count')

-output

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I am particulary looking for hist()
@EmmaVaze then you can change the barplot with hist
1

POSIXlt class already has year component in it which shows number of years since 1900. You can add 1900 to the year and use it in hist.

hist(trump_text$posted_at$year + 1900)

Using ggplot2 you can do :

library(dplyr)
library(ggplot2)

trump_text %>%
  mutate(year = format(posted_at, '%Y')) %>%
  ggplot() + aes(year) + geom_histogram(stat = 'count')

enter image description here

You can customize/update the plot based on your choice.

2 Comments

Must specify 'breaks' in hist(<POSIXt>) I get this error for break
Sorry, that should be hist(trump_text$posted_at$year + 1900). I have updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.