chenchenmomo
chenchenmomo

Reputation: 233

How to plot for a special data frame using R

The original data frame looks like as following:

status[1:15,]

     time                   dt_5
1   2014-07-31 17:00:01.0    0
2   2014-07-31 17:00:01.2    0
3   2014-07-31 17:00:01.4    1
4   2014-07-31 17:00:01.5    1
5   2014-07-31 17:00:02.2    1
6   2014-07-31 17:00:02.5    1
7   2014-07-31 17:00:03.0    1
8   2014-07-31 17:00:04.0    1
9   2014-07-31 17:00:04.0    1
10  2014-07-31 17:00:05.0    1
11  2014-07-31 17:00:09.0    1
12  2014-07-31 17:00:10.0    1
13  2014-07-31 17:00:10.2    1
14  2014-07-31 17:00:11.2    1
15  2014-07-31 17:00:11.5    1

To split it into groups by 3 mins interval, I used the George Dontas's solutionusing plyr to group and computed percentage of the value "1" in each groups.

percentage.occupied <- function(x) (NROW(subset(x,dt_5==0)))/(NROW(x))

splitbytime <- dlply(selectstatus327, .(cut(time,"3 mins")),percentage.occupied)

Then I got the result:

> splitbytime
$`2014-07-31 17:00:00`
[1] 0.2857143

$`2014-07-31 17:03:00`
[1] 0.2835821

$`2014-07-31 17:06:00`
[1] 0.6044304

$`2014-07-31 17:09:00`
[1] 0.5735294

$`2014-07-31 17:12:00`
[1] 0.8811475

$`2014-07-31 17:15:00`
[1] 0.599359

$`2014-07-31 17:18:00`
[1] 0.8021201

$`2014-07-31 17:21:00`
[1] 0.7956522

$`2014-07-31 17:24:00`
[1] 0.7022059

$`2014-07-31 17:27:00`
[1] 0.6568266

$`2014-07-31 17:30:00`
[1] 0.8390805

$`2014-07-31 17:33:00`
[1] 0.3838863

$`2014-07-31 17:36:00`
[1] 0.7686567

$`2014-07-31 17:39:00`
[1] 0.6580311

$`2014-07-31 17:42:00`
[1] 0.6923077

$`2014-07-31 17:45:00`
[1] 0.5813008

$`2014-07-31 17:48:00`
[1] 0.9066148

$`2014-07-31 17:51:00`
[1] 0.59375

$`2014-07-31 17:54:00`
[1] 0.9487179

$`2014-07-31 17:57:00`
[1] 0.7325581

My goal is to plot the percentage of value "1" by time(every 3 mins), but I don't know how to plot using the strange data frame (x_axis is time and y_axis is the percentage). I am quite a newbie (=^ ^=)

Upvotes: 0

Views: 43

Answers (1)

IRTFM
IRTFM

Reputation: 263301

You now have a list but not a dataframe. (If you wanted a dataframe then using ddply should have succeeded.) You could recover the "times" at least as a character vector with names(splitbytime). The original date-time value could be reconstituted with:

  as.POSIXct( names(splitbytime) )

The values could be extracted as a numeric vector with just:

 unlist( splitbytime )

So try:

  plot( x= as.POSIXct( names(splitbytime) ), 
        y= unlist( splitbytime ) ) 

Upvotes: 1

Related Questions