0

I have a time series(a csv file) data that looks like below. Each observation is spaced at interval of 2 seconds :

Group,Id, Observations (illustration only--csv file dont have headers)
A,21,2,3,4
B,12,2,9
C,13,3,5
A,14,4,6,7,9,12,3,4,5,6
B,15,3,5,2,1,3
C,16,8,67
A,27,98,78
B,18,2,3,4,5,6,7,8,1,2,3,4,5
C,19,2,3,4,5,6,6,4,3,3,2,3,4,4,5,6,66,6,4,3,232

Each row is a separate time series data. I am trying to compare and contrast these time series data w.r.t group(A,B,C) with some plots(at least for now).

Edit : I read the csv file as suggested in Import data into R with an unknown number of columns? which would read this csv as

 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21  X22
  A 21  2  3  4 NA NA NA NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
  B 12  2  9 NA NA NA NA NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
                      ---------------------------
  C 19  2  3  4  5  6  6  4   3   3   2   3   4   4   5   6  66   6   4   3  232

I tried to plot this data with ggplot by melting the data with

melt(data,c("X1","X2")) 
which gives me result
    X1 X2   variable value
1    A 21       X3     2
2    B 12       X3     2
3    C 13       X3     3
4    A 14       X3     4
5    B 15       X3     3
6    C 16       X3     8
7    A 27       X3    98
8    B 18       X3     2
9    C 19       X3     2
10   A 21       X4     3
11   B 12       X4     9
12   C 13       X4     5
13   A 14       X4     6
14   B 15       X4     5
15   C 16       X4    67
----------------------

X3,X4,X5..X22 are observation over period of time. I want to compare different groups(A,B,C) of data by independently plotting each observations(or say create time series for each observation).
I could not figure out plotting solution with zoo package. I was wondering how such plot can be generated for this type of time series data?

Community
  • 1
  • 1
iinception
  • 1,945
  • 2
  • 21
  • 19
  • It appears that you have more data points for group C compared to A and B. How do the extra data points fall in line with the other two groups? How does the second B row compare to the first? Should they be combined into one continuous time series? What was the result of `melt(data, ....`? Can you provide `dput(data)` after you read it in? – Chase Jul 08 '11 at 00:07
  • Above is just an illustration and number of observations are different for different group. I cannot combine groups(A,B or C) as observations are independent belonging to different groups and different ID and I am interested in observing behavior for each group separately. – iinception Jul 08 '11 at 01:54
  • So what is column x2 here? Is this like a subject ID, so you have multiple subjects within groups A, B and C? When you say in your comment to the answer below "Panel A would group all the time series observations belonging to group A", each row of your original data frame is a separate observation which you would like a line for? – nzcoops Jul 08 '11 at 02:58
  • yes, each row of my data frame is a separate observation that I want line for – iinception Jul 08 '11 at 05:27

1 Answers1

2

Try this:

Lines <- "A,21,2,3,4
B,12,2,9
C,13,3,5
A,14,4,6,7,9,12,3,4,5,6
B,15,3,5,2,1,3
C,16,8,67
A,27,98,78
B,18,2,3,4,5,6,7,8,1,2,3,4,5
C,19,2,3,4,5,6,6,4,3,3,2,3,4,4,5,6,66,6,4,3,232"

library(zoo)
k <- max(count.fields(textConnection(Lines), sep = ","))
DF <- read.table(textConnection(Lines), sep = ",", fill = TRUE, col.names = 1:k, 
   as.is = TRUE)

# next line may not be needed but won't hurt
DF[[1]] <- gsub(" ", "", DF[[1]])

z <- zooreg(t(DF[-1]), start = 0, deltat = 2)

plot(z, screen = DF[[1]])
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • thanks for the answer...this is not what I am looking for.basically I want three different panels say panel A,panel B,panel C. Panel A would group all the time series observations belonging to group A and so on for Panel B and C. I am interested in seeing how these groups differ from each other. Also, above zoo script reads this kind of csv file(that has variable length of column) incorrectly(the first column(which is Group) is scrambled).. – iinception Jul 08 '11 at 01:45
  • OK, the input portion is now corrected and the plot modified in light of your clarification of what you want so that it now produces 3 panels. – G. Grothendieck Jul 08 '11 at 02:57