1

I have a time series panel dataset similar to the following format, where V1 is character (here A,B,C...) and V2 is numeric(5,8,6...). I have 65 different items in V1 (total of about 50000 odd observations). I am trying to find a way to plot V2 provided V1=A or B and so on. What I can simply do is plot(V1[1:1065]), plot([V1:1066:2085]) and so on but I was looking for a robust way of doing this. Tried something like this:

if(V1 == 'A') plot(V2)

But since 'if' in R does not accept vectors, it gives an error message, "the condition has length > 1 and only the first element will be used"

V1 V2      Date

A   5   01/01/2014

A   8   08/01/2014

B   6   15/01/2014

C   9   22/01/2014

C   6   29/01/2014

D   3   05/02/2014

-   -   -

-   -   -

-   -   -

-   -   -

X   8   12/03/2014

Y   5   19/03/2014

Z   5   26/03/2014

Could anyone please suggest something?

gagolews
  • 12,836
  • 2
  • 50
  • 75
Shraddha
  • 155
  • 3
  • 16
  • Do you want 65 separate plots or several/all lines per unique value of V1 combined in one plot? – talat Jun 24 '14 at 17:02
  • Did you `attach` your data? Your use of the column names inside the `if` looks like that. Just note that it is widely discouraged to use `attach` when scripting. Better learn how to subset using `[]` and `$`. – talat Jun 24 '14 at 18:07
  • Thanks for the tip! I am using $ now instead of attach .. – Shraddha Jul 01 '14 at 11:12
  • 1
    Good to hear that. Make sure you also check out `?with`, which is also helpful in many situations. – talat Jul 01 '14 at 11:30

1 Answers1

2

Assuming these are all stored in a data.frame called dd then you could do

plot(V2~Date, data=subset(dd, V1=="A"))

That should produce a plot for all the "A" values in V1.

MrFlick
  • 195,160
  • 17
  • 277
  • 295