Note that there are some explanatory texts on larger screens.

plurals
  1. POR: Extracting proper time-date out of csv
    primarykey
    data
    text
    <p>I have a csv with some time series data with date-time formatted like this:</p> <pre><code>mydata &lt;- read.csv("mydata.csv") &gt; mode(mydata$t_5min[1]) [1] "numeric" </code></pre> <p>It looks like R is interpreting it as a factor, since it can't understand the format:</p> <pre><code>mydata$t_5min[1] [1] 1/3/2012 16:00 27698 Levels: 10/10/2012 10:00 10/10/2012 10:05 10/10/2012 10:10 10/10/2012 10:15 ... 9/6/2012 9:55 </code></pre> <p>I tried using strptime, which seems to work okay on single entries:</p> <pre><code>&gt; strptime(x=mydata$t_5min[2],format="%d/%m/%Y %H:%M", tz="") [1] "2012-04-01 06:10:00" &gt; mode(strptime(x=mydata$t_5min[2],format="%d/%m/%Y %H:%M", tz="")) [1] "list" </code></pre> <p>But if I try this with sapply, I get the following error:</p> <pre><code>mydata$t_5min &lt;- sapply(mydata$t_5min, strptime, format="%d/%m/%Y %H:%M", tz="") Error in `$&lt;-.data.frame`(`*tmp*`, "t_5min", value = list(sec = 0, min = 0L, : replacement has 9000 rows, data has 1000 </code></pre> <p>I tried the timeDate library with slightly better results:</p> <pre><code>&gt; as.timeDate(mydata$t_5min[1]) GMT [1] [2012-01-03] </code></pre> <p>However, I need minute precision. However, the example code in the timeDate function doesn't seem to work (or I'm using it wrong, but it was kind of brief):</p> <pre><code>as.timeDate(mydata$t_5min[2], units=c("min")) Error in as.timeDate(mydata$t_5min[2], units = c("min")) : unused argument(s) (units = c("min")) </code></pre> <p><strong>What's the right way to do convert this time data into something R can work with?</strong></p> <p>Here is some data to duplicate these results:</p> <pre><code>t_5min,n,value 1/3/2012 16:00,16,48.125 1/3/2012 16:05,28,44.39285714 1/3/2012 16:10,29,37.44827586 1/3/2012 16:15,28,30.39285714 1/3/2012 16:20,28,23.67857143 1/3/2012 16:25,29,19.10344828 1/3/2012 16:30,28,16.35714286 1/3/2012 16:35,29,14.34482759 1/3/2012 16:40,28,11.71428571 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload