0

I have a dataset of names and values, e.g.

Date    Name    Value
01/01/17    Miles ran   1.2
02/01/17    Miles ran   1.2
03/01/17    Miles ran   1.3
04/01/17    Miles ran   1.4
05/01/17    Miles ran   1.4
06/01/17    Miles ran   1.6
07/01/17    Miles ran   1.5
08/01/17    Miles ran   1.8
09/01/17    Miles ran   1.7
10/01/17    Miles ran   2.1
01/01/17    Calories consumed   2300
02/01/17    Calories consumed   2200
03/01/17    Calories consumed   2250
04/01/17    Calories consumed   2410
05/01/17    Calories consumed   1980
06/01/17    Calories consumed   2000
07/01/17    Calories consumed   1900
08/01/17    Calories consumed   2400
09/01/17    Calories consumed   2150
10/01/17    Calories consumed   1900

I want to be able to run various calculations on the data, such as being able to run a forecast() function on the data for each separate time series in the panel data (dates already defined).

However, I am unsure as to how to loop using subset. e.g. I have to define the name reference for the subset each time, and would rather achieve this by means of a loop which runs the calculation on each subset in turn.

This is my current code:

listofids=as.character(unique(mydata$Name))
mylist1 <- split(mydata, mydata$Name)
mylist1
df1<-data.frame(mylist[1])

listofids=as.character(unique(mydata$Name))
mylist2 <- split(mydata, mydata$Name)
mylist2
df2<-data.frame(mylist[2])

forecast(df1$Value,h=365-Number_of_Days)

The idea is to segment the panel data into separate datasets, and then conduct the forecasts. However, as can be seen above, I would need to run separate forecasts for each separate data frame and want to loop this instead. Any help would be greatly appreciated.

yellowlemon
  • 43
  • 2
  • 7

1 Answers1

0

Using plyr, you should group by "Name" and then use mutate.

my.summaries <- ddply(mydataedited, .(Name), mutate, cumeValue = cumsum(Value))

     Name Value cumeValue
1   Apple    41        41
2   Apple    22        63
3   Apple    11        74
4   Apple    13        87
5   Apple    37       124
6   Apple    26       150
7   Apple    13       163
8   Apple    45       208
9   Apple    31       239
10 Banana     9         9
11 Banana    10        19
12 Banana    11        30
13 Banana    17        47
14 Banana    10        57
15 Banana    24        81
16 Banana    27       108
17 Banana    25       133
18 Banana    28       161
19 Banana    34       195
20 Banana    46       241
jdobres
  • 11,339
  • 1
  • 17
  • 37