R Sparklines package error

143 Views Asked by At

I am using the Sparklines package in R and was using the reshapeExt() function to prepare a dataset, but ran into a very very strange issue. The dataset looks like this:

      Company time value
1   Microsoft 1990    11
2 Time Warner 1990    22
3     Verizon 1990    33
4   Microsoft 1991    44
5 Time Warner 1991    55
6     Verizon 1991    66
7   Microsoft 1992    77
8 Time Warner 1992    88
9     Verizon 1992    99

And then I followed the tutorial example to run these lines:

example <- example[,c("Company","value","time")]
example$time <- as.numeric ( as.character ( example$ time ))
dat <- reshapeExt ( example , idvar =" Company " , varying = list (2))

Inexplicably, R gave me this for "dat":

      Company time value  Company 
1   Microsoft    1    11         1
2 Time Warner    1    22         2
3     Verizon    1    33         3
4   Microsoft    1    44         4
5 Time Warner    1    55         5
6     Verizon    1    66         6
7   Microsoft    1    77         7
8 Time Warner    1    88         8
9     Verizon    1    99         9

The time column all became 1 for no apparent reason. This does not happen when I was implementing the example at https://web.warwick.ac.uk/statsdept/user2011/TalkSlides/Contributed/18Aug_0950_FocusVI_4-ReportingData_2-Kowarik.pdf page 18 - what's happening here?

Any help is appreciated

1

There are 1 best solutions below

0
On

8 months after, I don't know if it is useful for you, but, I was having a similar problem and I found your question. Maybe this will help other people anyway.

First, certify that you have only 3 columns in this order: company, time, value

I think the problem is because the order of the data, you need to order by company and time:

example <- examplewith(example, order(Company, time)), ]

then the data would be like this:

      Company time value
1   Microsoft 1990    11
4   Microsoft 1991    44
7   Microsoft 1992    77
2 Time_Warner 1990    22
5 Time_Warner 1991    55
8 Time_Warner 1992    88
3     Verizon 1990    33
6     Verizon 1991    66
9     Verizon 1992    99

Then use reshapeExt, and change the varying = list(3) to 3, because your values are in the third column.

example2 <- reshapeExt(example, idvar = "Company", varying = list(3))

The final result is:

      Company time value
1   Microsoft    1    11
4   Microsoft    2    44
7   Microsoft    3    77
2 Time_Warner    1    22
5 Time_Warner    2    55
8 Time_Warner    3    88
3     Verizon    1    33
6     Verizon    2    66
9     Verizon    3    99

I think will work this way. Thanks!