hi, I have a and data frame with date-column and some other columns. My first question is what is the fastest way to get the index of an array if I know the value f.e> x = c(4,5,6,7,8)so i know the value is 6.. i.e. the index is 3. What I currently do is loop over the array, I was thinking if there is faster more direct way. The next one...is I have a data frame one of the columns is Date based (stored as string), as you may be guessed I have the date and I want to find the index ;), but here is one more complication. The dates are not sequential, but only dates when the day is Mon-Fri i.e. for Sat and Sun i don't store information. So I have first convert the date I have into the closest Monday. Let me give you one example. Let say I have the date 2000/01/01 (Sat), now to be able to find any information I have to find the nearest Monday in this case it is 2000/01/03 (Mon).. So now that I have this new date I can find the index of the element in the array where it is stored and from this I can get the real data I need. In short conversation is from Data ==> nearest Monday ==> index of the element in the array where it is stored. thank you very much
On Wed, May 13, 2009 at 4:23 PM, myshare <mraptor@gmail.com> wrote:> hi, > > I have a and data frame with date-column and some other columns. > My first question is what is the fastest way to get the index of an > array if I know the value f.e > > > x = c(4,5,6,7,8) > > so i know the value is 6.. i.e. the index is 3. What I currently do is > loop over the array, I was thinking if there > is faster more direct way.which(x == 6) will give you the index.> > The next one...is I have a data frame one of the columns is Date based > (stored as string), as you may be guessed > I have the date and I want to find the index ;), but here is one more > complication. > The dates are not sequential, but only dates when the day is Mon-Fri > i.e. for Sat and Sun i don't store information. > > So I have first convert the date I have into the closest Monday. > Let me give you one example. Let say I have the date 2000/01/01 (Sat), > now to be able to find any information I have to find the nearest > Monday in this case it is 2000/01/03 (Mon).. > So now that I have this new date I can find the index of the element > in the array where it is stored and from this I can get the real data > I need. > In short conversation is from Data ==> nearest Monday ==> index of the > element in the array where it is stored.Here is a way of adjusting a date to the nearest Monday if it is a weekend:> x <- seq(as.Date('2009-05-01'), by='1 day', length=30) > x[1] "2009-05-01" "2009-05-02" "2009-05-03" "2009-05-04" "2009-05-05" "2009-05-06" "2009-05-07" [8] "2009-05-08" "2009-05-09" "2009-05-10" "2009-05-11" "2009-05-12" "2009-05-13" "2009-05-14" [15] "2009-05-15" "2009-05-16" "2009-05-17" "2009-05-18" "2009-05-19" "2009-05-20" "2009-05-21" [22] "2009-05-22" "2009-05-23" "2009-05-24" "2009-05-25" "2009-05-26" "2009-05-27" "2009-05-28" [29] "2009-05-29" "2009-05-30"> x.new <- x + ifelse(weekdays(x) == "Saturday", 2, ifelse(weekdays(x) ="Sunday", 1, 0)) > x.new[1] "2009-05-01" "2009-05-04" "2009-05-04" "2009-05-04" "2009-05-05" "2009-05-06" "2009-05-07" [8] "2009-05-08" "2009-05-11" "2009-05-11" "2009-05-11" "2009-05-12" "2009-05-13" "2009-05-14" [15] "2009-05-15" "2009-05-18" "2009-05-18" "2009-05-18" "2009-05-19" "2009-05-20" "2009-05-21" [22] "2009-05-22" "2009-05-25" "2009-05-25" "2009-05-25" "2009-05-26" "2009-05-27" "2009-05-28" [29] "2009-05-29" "2009-06-01">> > > thank you very much > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
There is a one line nextfri function defined in the zoo-quickref vignette and replacing 5 with 1 in its definition will give you next Monday. The prior Monday is 7 days before that so take the closest of the two. See R News 4/1 for more about dates. On Wed, May 13, 2009 at 4:23 PM, myshare <mraptor at gmail.com> wrote:> hi, > > I have a and data frame with date-column and some other columns. > My first question is what is the fastest way to get the index of an > array if I know the value f.e > >> x = c(4,5,6,7,8) > > so i know the value is 6.. i.e. the index is 3. What I currently do is > loop over the array, I was thinking if there > is faster more direct way. > The next one...is I have a data frame one of the columns is Date based > (stored as string), as you may be guessed > I have the date and I want to find the index ;), but here is one more > complication. > The dates are not sequential, but only dates when the day is Mon-Fri > i.e. for Sat and Sun i don't store information. > > So I have first convert the date I have into the closest Monday. > Let me give you one example. Let say I have the date 2000/01/01 (Sat), > now to be able to find any information I have to find the nearest > Monday in this case it is 2000/01/03 (Mon).. > So now that I have this new date I can find the index of the element > in the array where it is stored and from this I can get the real data > I need. > In short conversation is from Data ==> nearest Monday ==> index of the > element in the array where it is stored. > > thank you very much > > ______________________________________________ > 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. >