iinception - 1 year ago 68

R Question

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`

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?

Answer Source

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]])
```