why is this for() loop not outputting as i'm expecting

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

why is this for() loop not outputting as i'm expecting

Allerious
d

#The above 'd' is a data frame which outputs the following table containing 14 obs and 2 vars (NUMBER and DATE).

NUMBER       DATE
1      20    2017-01-01
2      30    2017-01-02
3      40    2017-01-03
4      40    2017-01-04
5      50    2017-01-05
6      50    2017-01-06
7      60    2017-01-07
8      20    2017-01-08
9      30    2017-01-09
10     40   2017-01-10
11     40   2017-01-11
12     50   2017-01-12
13     50    2017-01-13
14     60    2017-01-14

#After running the following code:

a<-c(0,7)

for(i in a){

 w <- subset(d, DATE >= as.Date("2017-01-01")+a & DATE <= as.Date("2017-01-
07")+a )

print(w)
}

#I get the following output. I was expecting the output to be two tables with the first dates 1st-7th and the second 8th-14th. If i change the 'a' variable in the code to just '0' or just '7' it will run the dates as i'd expect (but obviously only 1 table each time i run the for loop).

NUMBER       DATE
1      20 2017-01-01
3      40 2017-01-03
5      50 2017-01-05
7      60 2017-01-07
8      20 2017-01-08
10     40 2017-01-10
12     50 2017-01-12
14     60 2017-01-14
   NUMBER       DATE
1      20 2017-01-01
3      40 2017-01-03
5      50 2017-01-05
7      60 2017-01-07
8      20 2017-01-08
10     40 2017-01-10
12     50 2017-01-12
14     60 2017-01-14

So how do i get the output to show table 1: 1st-7th dates and table 2: 8th-14th dates?
Reply | Threaded
Open this post in threaded view
|

Re: why is this for() loop not outputting as i'm expecting

Allerious
Oh just realised my mistake shortly after posting this. I had put 'a' instead of 'i' inside the for loop.