Plot a histogram of yearly counts Strptime()

Hello, I have a csv file that consists of one column. The column presents the date of posting on a website. I want to plot a histogram to see how the number of posts varies over the years. The file contains the years (2012 to 2016) and consists of 11,000 rows.

sample of the file:

2 30/1/12 21:07
3 2/2/12 15:53
4 3/4/12 0:49
5 14/11/12 3:49
6 11/8/13 16:00
7 31/7/14 8:08
8 31/7/14 10:48
9 6/8/14 9:24
10 16/12/14 3:34

The data types is dataframe
class(postsData) [1] "data.frame"

I tried converting the data to text using strptime function as below:

formatDate <- strptime(as.character(postsData$Date),format="“%d/%m/%y")

then plot the histogram

hist(formatDate,breaks=10,xlab="year")

image

Since this is a university assignment question I have to use strptime() to do the conversion and not any other function. The specification says:

you’ll need to convert from text values using the strptime() function. Instructions on how to use the function is available here:

R: Date-time Conversion Functions to and from Character

You will need to write a format string, starting with “%a %b” to tell the function how to parse the particular date/time format in your file. What format string do you need to use?

Thank you,

It seems like you have already managed to plot the requested histogram, could you explain what your specific problem is? Also please take a look to our homework policy, you are not supposed to include verbatim instructions.

Thanks for letting us know this is a homework question.

Firstly, your challenge with formatDate <- strptime(as.character(postsData$Date),format="“%d/%m/%y")
is

  • note the typo of the extra in the format argument
  • In format you need to get the right conversion specifications. Note the example, "16/12/14 3:34", that appears to be day/month/year hour:minute. Have a look at your link for all the conversation specifications.

Once you get the date-time extracted from the string, you want to extract just the year. Here's a good discussion of that: https://stackoverflow.com/questions/31973673/how-can-i-extract-year-from-strptime

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.