Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Wednesday, December 03, 2014

Converting Google Trends weekly data into a regular date in R

Getting dates in the right format is always a big headache when getting your data ready for analysis. Google Trends provides data on three levels, monthly, weekly or daily. Here, I explain how to convert Google Trend's weekly dates into R's date class.

The weekly date format used by Google Trends looks like this:

2004-01-04 - 2004-01-10
 

This might pose a problem for instance if we want to plot the data. We will need to convert the date interval provided by Google Trends into a single date. We can do so using the following code:

#First, import the data 
data=read.csv(filePath, header=F, blank.lines.skip=F)
 
#Then select the ending date of the date interval
data[,1]=sapply(data[,1], substr, start=14, stop=30)
 
#And convert it into a date 
data[,1]=as.Date(data[,1], "%Y-%m-%d")
 
And then you have a date format that you can use for plots or data analysis.


 
Entertaining Blogs - BlogCatalog Blog Directory
Bloggtoppen.se