Twitter plot created time - r

I'm trying to plot the created time of twitters. I can extract the created time with the following code:
tweets <- searchTwitter('weather', n=100,lang='en')
t <- twListToDF(tweets)
s <- t[, c("created")]
The time format I get is something like: 2017-02-25 18:52:06 UTC
Trying to plot it with plot(s) provides just a list of dots. I'm not sure if it is due to the date/time format.
I want to create a barchart that each bar represents the count of tweets on an hourly period. The x axis would represent time and the y axis the number of tweets.
Any ideas?

One approach that I've used uses the 'lubridate' package, which is available on CRAN.
library(lubridate)
date(now())
hour(now())
You would replace now() with your vector s. If your s vector has the class POSIXct, I believe that this will work. There might be alternative solutions that involve the lubridate R package, too. I hope that this helps.

Related

How to get dates on the xaxis of my Arima forecast plot rather than just numbers

I have imported a netCDF file into R and created a dataset which has 58196 time stamps. I’ve then fitted an Arima model to it and forecasted. However, the format of the time is ‘hours since 1900-01-01 00:00:00’. Each of the times are just in a numerical order up to 58196, but I would like to use ggplot to plot the forecast with dates on the xaxis.
Any ideas? Here is some code I have put in.
I have read in the required variable and taken it along what pressure level I want, so that it is a single variable at 58169 times, 6hourly intervals up to the end of the year in 2018. I have then done the following:
data <- data_array[13, ] # To get my univariate time series.
print(data)
[58176] -6.537371e-01 -4.765177e-01 -4.226107e-01 -4.303621e-01
-3.519134e-01
[58181] -2.706966e-01 -1.864843e-01 -9.974014e-02 2.970415e-02
6.640909e-02
[58186] -1.504763e-01 -3.968417e-01 -4.864971e-01 -5.934973e-01
-7.059880e-01
[58191] -7.812654e-01 -7.622807e-01 -8.968482e-01 -9.414597e-01
-1.003678e+00
[58196] -9.908477e-01
datafit <- auto.arima(data)
datamodel <- Arima(data, order = c(5, 0, 2))
datafcst <- forecast(datamodel, h=60, level=95)
plot(datafcst, xlim=c(58100, 58250))
enter image description here
I have attached the image it yields too. The idea is that I can use ggplot to plot this rather than the standard plot, with dates on the xaxis instead of the numerical values. However, ggplot also won't work for me as it says it isn't considered a data frame?
Many thanks!
as you did not provide a minimal example it is hard to help you but I try. Assume your date is called "date".
dater = as.Date(strptime(date, "%Y-%m-%d"))
And from ?strptime:
format
A character string. The default for the format methods is "%Y-%m-%d %H:%M:%S" if any element has a time component which is not midnight, and "%Y-%m-%d" otherwise.
Hope that helps

R Timeline Without Dates

I'm trying to make a timeline like you'd make with any of the timevis, vistime, or timeline R packages, but I'm only interested in times and not dates. I don't mind putting a placeholder date in there, but it seems that all of these packages require the start and end times to include dates and include the date in the timeline.
I've been searching for ways to either not include dates in a timeline or only print the time but not the date in any of these package, but haven't been able to find anything. Does anyone have any ideas?
All of those packages use as.POSIXct under the hood, which requires objects to be Date objects and doesn't work with times only. So, if your data is about only one day, you can add the date on the clock times (using paste) and e.g. vistime will display only the time (ok, a date almost completely hidden in the corner):
dat <- data.frame(event = 1:2,
start = c("14:00", "16:00"),
end = c("15:30", "17:00"))
# add a Date
dat[,c("start", "end")] <- sapply(dat[,c("start", "end")], function(x) paste(Sys.Date(), x))
vistime(dat)
I use vistime version 0.7.0.9000 which can be obtained by executing devtools::install_github("shosaco/vistime").
If you want to represent times without any date information, you should try out the package hms. It is part of the tidyverse collection and is described as:
A simple class for storing durations or time-of-day values and displaying them in the hh:mm:ss format.
Example use:
library(hms)
hms(56, 34, 12)
#> 12:34:56

Processing Accelerometer Data in R

I have accelerometer data Log, the data contain (accX, accY, accZ,timestamp)
The data look like
I am so confused how to processing it in R.
I have two questions:
Is there any library for handle this data? I want plot it like time
series data and analysis it.
How to process the timestamp? because the timestamp is not every
second but milisecond.
Please anyone can give me some light?
Thank you in advance
To answer your two specific questions:
1) There are many packages for time series analysis. See here: http://cran.r-project.org/web/views/TimeSeries.html
2) There are many ways to process the time stamp data. #bjoseph gives you some very good advice in his response. The lubridate package (http://cran.r-project.org/web/packages/lubridate/index.html) is very good at handling time data with somewhat more sensible functions than the POSIX set. ggplot2 (http://ggplot2.org/) plots time series data quite sensibly as well.
The classes POSIXlt and POSIXct exist in R for manipulating calendar times and dates. See more information here: http://stat.ethz.ch/R-manual/R-devel/library/base/html/as.POSIXlt.html
In your specific case you need to may need to modify your system time to include milliseconds:
test <- "2014-07-09 15:03:33:252"
test
[1]"2014-07-09 15:03:33:252"
options("digits.secs"=6)
Sys.time()
[1] "2014-07-23 11:16:32.480932 EDT"
as.POSIXlt(test)
[1] "2014-07-09 15:03:33 EDT"
test
[1] "2014-07-09 15:03:33:252"
time.test <- as.POSIXlt(test)
class(time.test)
[1] "POSIXlt" "POSIXt"
time.test
[1] "2014-07-09 15:03:33 EDT"
To apply this to the entire column of your data.table you can run:
dt$timecolumn <- as.POSIXlt(dt$timecolumn)
where time column is the name of the column with the times in it.
If you need help importing the data from excel, a good guide:
> library(gdata) # load gdata package
> help(read.xls) # documentation
> mydata = read.xls("mydata.xls") # read from first sheet

R - How to work with and plot time data

I have the following data (of which the following is a small sample):
times <- c("02:45:00", "02:45:07", "02:45:10", "02:45:20", "02:45:25", "02:45:27", "02:45:27", "02:45:30", "02:45:32", "02:45:37")
I would like to plot these times and be able to have them be in a time variable format if possible. In the graph, I want to be able to have different time bands in order to create a histogram of the different distribution of times.
You could look into strptime to get familiar with the base time format.
Then, something like this might get you started:
hist(strptime(times,"%H:%M:%S"), "secs", freq = TRUE, xlab="seconds")

How to plot a range of data with time in hh:mm:ss.000 format in R?

I have a set of data that need to be plotted (1M rows) with R. The time column (column 1) is in hh:mm:ss.000 format. I would like to plot the graph in a time range, say from 08:05:00 to 09:00:00. How do I do it? I have searched the web and couldn't find a way to set the xlim properly.
Here's a short example of the data. Column 1 is time, Column 2, 3, 4.. will be on y axis.
07:51:19.553,10.785,0.000,0.392,1.512,1.527,1.553,1.560,2.838
08:05:00.661,-1.555,0.000,0.041,0.310,0.314,0.321,0.327,1.474
08:06:58.250,30.781,0.000,0.093,0.156,0.160,0.168,0.173,1.411
08:30:02.506,-0.002,0.000,0.052,0.120,0.123,0.132,0.137,1.361
09:05:00.997,-1.802,0.000,0.032,0.078,0.080,0.087,0.090,1.258
10:05:00.661,-1.555,0.000,0.041,0.310,0.314,0.321,0.327,1.474
Thanks in advance for your help.
You really want to use a proper time series class such as zoo or xts
Subsetting, plotting, ... then come for free. Start with the excellent zoo documentation before maybe switching to xts for even better performance and subsetting.
Now, one million rows is too many as you end up with more data than pixels -- but at least this will give you a chance to summarize your data.
Here is a quick illustration:
> options(digits.sec=3) ## important: turn on milli-sec via print()
> library(xts)
Loading required package: zoo
> X <- xts(cumsum(rnorm(100)), order.by=Sys.time()+cumsum(runif(100)/10))
> plot(X)
To change character vector to "date & time" object, POSIXlt(ct) object, function strptime() will come handy. Here's a short example how it's done.
dtm <- strptime(c("1.1.2010 11:35"), format = "%d.%m.%Y %H:%M", tz = "CET")

Resources