I all I am just trying to get the idea of a loop if statement for another purpose. I was going to label a new variable based on date ie first week 1, second week 2 etc. My code in bold is wrong but seems logical to me, could someone help. x <- rep(c(2660156,2663703,2658165,2659303,2661531,2660914),c(2,2,2,2,1,1)) y <- rep(c(6476767,6475013,6475487,6479659,6477004,6476388),c(2,2,2,2,1,1)) date <- rep(as.Date(c("2008-01-01","2008-01-14","2008-01-28"), format = "%Y-%m-%d"),c(4,4,2)) a<-data.frame(x,y,date) a$wk<-rep(c(0),nrow(a)) a set<-as.Date(c("2008-01-01"), format = "%Y-%m-%d") set for(i in a$date){ if (a$date[i]>set & a$date[i]<set +7 ){a$wk<-1}else if (a$date[i]>set +7){a$wk<-2}else {a$wk<-3} } a Kind regards andy Andrew McFadden MVS BVSc Incursion Investigator Investigation & Diagnostic Centres - Wallaceville Biosecurity New Zealand Ministry of Agriculture and Forestry Phone 04 894 5600 Fax 04 894 4973 Mobile 029 894 5611 Postal address: Investigation and Diagnostic Centre- Wallaceville Box 40742 Ward St Upper Hutt ######################################################################## This email message and any attachment(s) is intended solely for the addressee(s) named above. The information it contains is confidential and may be legally privileged. Unauthorised use of the message, or the information it contains, may be unlawful. If you have received this message by mistake please call the sender immediately on 64 4 8940100 or notify us by return email and erase the original message and attachments. Thank you. The Ministry of Agriculture and Forestry accepts no responsibility for changes made to this email or to any attachments after transmission from the office. ######################################################################## [[alternative HTML version deleted]]
The bolding is lost in plain-text email, but some things to note: :: for(i in a$date){ - this will make i take on date values, you probably meant i to index the rows of a[] :: you missed indexing in your replacements in the then and else clauses :: you probably want some equalities in your test conditions, not all '<' '>' inequalities :: need an extra condition for the 2 weeks test clause for(i in seq(nrow(a))){ if (a$date[i]>=set & a$date[i]<set +7 ){a$wk[i]<-1}else if (a$date[i]>=set +7 & a$date[i]<set +14 ){a$wk[i]<-2}else {a$wk[i]<-3} }> ax y date wk 1 2660156 6476767 2008-01-01 1 2 2660156 6476767 2008-01-01 1 3 2663703 6475013 2008-01-01 1 4 2663703 6475013 2008-01-01 1 5 2658165 6475487 2008-01-14 2 6 2658165 6475487 2008-01-14 2 7 2659303 6479659 2008-01-14 2 8 2659303 6479659 2008-01-14 2 9 2661531 6477004 2008-01-28 3 10 2660914 6476388 2008-01-28 3 -----Original Message----- From: r-help-bounces at r-project.org on behalf of Andrew McFadden Sent: Thu 6/5/2008 8:46 PM To: r-help at r-project.org Subject: [R] R loop I all I am just trying to get the idea of a loop if statement for another purpose. I was going to label a new variable based on date ie first week 1, second week 2 etc. My code in bold is wrong but seems logical to me, could someone help. x <- rep(c(2660156,2663703,2658165,2659303,2661531,2660914),c(2,2,2,2,1,1)) y <- rep(c(6476767,6475013,6475487,6479659,6477004,6476388),c(2,2,2,2,1,1)) date <- rep(as.Date(c("2008-01-01","2008-01-14","2008-01-28"), format = "%Y-%m-%d"),c(4,4,2)) a<-data.frame(x,y,date) a$wk<-rep(c(0),nrow(a)) a set<-as.Date(c("2008-01-01"), format = "%Y-%m-%d") set for(i in a$date){ if (a$date[i]>set & a$date[i]<set +7 ){a$wk<-1}else if (a$date[i]>set +7){a$wk<-2}else {a$wk<-3} } a Kind regards andy Andrew McFadden MVS BVSc Incursion Investigator Investigation & Diagnostic Centres - Wallaceville Biosecurity New Zealand Ministry of Agriculture and Forestry Phone 04 894 5600 Fax 04 894 4973 Mobile 029 894 5611 Postal address: Investigation and Diagnostic Centre- Wallaceville Box 40742 Ward St Upper Hutt ######################################################################## This email message and any attachment(s) is intended solely for the addressee(s) named above. The information it contains is confidential and may be legally privileged. Unauthorised use of the message, or the information it contains, may be unlawful. If you have received this message by mistake please call the sender immediately on 64 4 8940100 or notify us by return email and erase the original message and attachments. Thank you. The Ministry of Agriculture and Forestry accepts no responsibility for changes made to this email or to any attachments after transmission from the office. ######################################################################## [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi Andy, I see two problems: First of all, it must be a$wk[i] <- 1 and not a$wk <- 1, since this way it makes all 10 entries of a$wk to be 1. But R won't complain about this! What R should complain about is your loop. If you write for (i in a$date) it means that i loops between all the VALUES in a$date, not their indexes (i.e. i will be "2008-01-01" 4 times, "2008-01-14" 4 times, etc). So replace your loop by for (i in 1:length(a$date)) or alternatively your condition should be if (i > set) and not if (a$date[i] > set) since a$date[as.Date("2008-01-01")] doesn't make much sense. But if you do this you will have problem changing the appropriate value of a$wk. --- On Fri, 6/6/08, Andrew McFadden <Andrew.McFadden at maf.govt.nz> wrote:> From: Andrew McFadden <Andrew.McFadden at maf.govt.nz> > Subject: [R] R loop > To: r-help at r-project.org > Received: Friday, 6 June, 2008, 1:46 PM > I all > > I am just trying to get the idea of a loop if statement for > another > purpose. I was going to label a new variable based on date > ie first week > 1, second week 2 etc. My code in bold is wrong but seems > logical to me, > could someone help. > > x <- > rep(c(2660156,2663703,2658165,2659303,2661531,2660914),c(2,2,2,2,1,1)) > y <- > rep(c(6476767,6475013,6475487,6479659,6477004,6476388),c(2,2,2,2,1,1)) > date <- > rep(as.Date(c("2008-01-01","2008-01-14","2008-01-28"), > format > = "%Y-%m-%d"),c(4,4,2)) > a<-data.frame(x,y,date) > a$wk<-rep(c(0),nrow(a)) > a > set<-as.Date(c("2008-01-01"), format > "%Y-%m-%d") > set > > for(i in a$date){ > if (a$date[i]>set & a$date[i]<set +7 > ){a$wk<-1}else > if (a$date[i]>set +7){a$wk<-2}else {a$wk<-3} > } > a > > Kind regards > > andy > > Andrew McFadden MVS BVSc > Incursion Investigator > Investigation & Diagnostic Centres - Wallaceville > Biosecurity New > Zealand Ministry of Agriculture and Forestry > > Phone 04 894 5600 Fax 04 894 4973 Mobile 029 894 5611 > Postal address: > Investigation and Diagnostic Centre- Wallaceville Box 40742 > Ward St > Upper Hutt > > > ######################################################################## > This email message and any attachment(s) is intended solely > for the > addressee(s) named above. The information it contains is > confidential > and may be legally privileged. Unauthorised use of the > message, or the > information it contains, may be unlawful. If you have > received this > message by mistake please call the sender immediately on 64 > 4 8940100 > or notify us by return email and erase the original message > and > attachments. Thank you. > > The Ministry of Agriculture and Forestry accepts no > responsibility for > changes made to this email or to any attachments after > transmission from > the office. > ######################################################################## > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code.
Note that "%Y-%m-%d" is the default format so you can omit it. Perhaps you want something along these lines (modify formula to get the precise numbering you like): a$wk <- julian(a$date, as.Date("2008-01-01")) %/% 7 Also note %W can be used as a format string. See ?strptime for the codes. R News 4/1 has an article about dates. On Thu, Jun 5, 2008 at 11:46 PM, Andrew McFadden <Andrew.McFadden at maf.govt.nz> wrote:> I all > > I am just trying to get the idea of a loop if statement for another > purpose. I was going to label a new variable based on date ie first week > 1, second week 2 etc. My code in bold is wrong but seems logical to me, > could someone help. > > x <- > rep(c(2660156,2663703,2658165,2659303,2661531,2660914),c(2,2,2,2,1,1)) > y <- > rep(c(6476767,6475013,6475487,6479659,6477004,6476388),c(2,2,2,2,1,1)) > date <- rep(as.Date(c("2008-01-01","2008-01-14","2008-01-28"), format > = "%Y-%m-%d"),c(4,4,2)) > a<-data.frame(x,y,date) > a$wk<-rep(c(0),nrow(a)) > a > set<-as.Date(c("2008-01-01"), format = "%Y-%m-%d") > set > > for(i in a$date){ > if (a$date[i]>set & a$date[i]<set +7 ){a$wk<-1}else > if (a$date[i]>set +7){a$wk<-2}else {a$wk<-3} > } > a > > Kind regards > > andy > > Andrew McFadden MVS BVSc > Incursion Investigator > Investigation & Diagnostic Centres - Wallaceville Biosecurity New > Zealand Ministry of Agriculture and Forestry > > Phone 04 894 5600 Fax 04 894 4973 Mobile 029 894 5611 Postal address: > Investigation and Diagnostic Centre- Wallaceville Box 40742 Ward St > Upper Hutt > > > ######################################################################## > This email message and any attachment(s) is intended solely for the > addressee(s) named above. The information it contains is confidential > and may be legally privileged. Unauthorised use of the message, or the > information it contains, may be unlawful. If you have received this > message by mistake please call the sender immediately on 64 4 8940100 > or notify us by return email and erase the original message and > attachments. Thank you. > > The Ministry of Agriculture and Forestry accepts no responsibility for > changes made to this email or to any attachments after transmission from > the office. > ######################################################################## > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >