[R] Sum of two consecutive number in dataset.
Tom Wright
tom at maladmin.com
Wed Oct 15 16:15:42 CEST 2014
A couple of observations:
1) I'm not sure what the variable i is doing, looks like you are trying
to loop through years but perhaps you left that bit of code out for
clarity.
2) On the first loop of i you are assigning the values of
Samaru56[sow_day,] to all values in Samaru56. For future loops all
values on Samaru56 will be the same.
My approach to this problem would be:
rowCount<-nrow(Samaru56)
Samaru56$two_day<-vector(length=rowCount)
#first day doesnt have a two day value
Samaru56$two_day[2:rowCount]<-
Samaru56$Rain[1:rowCount-1] + Samaru56$Rain[2:rowCount]
sow_day <- min(c(
which(Samaru56$Rain > 20 & Samaru$Day>91),
which(Samaru56$two_day > 20 & Samaru$Day>91)))
#first day doesnt have a two day value
On Wed, 2014-10-15 at 10:01 +0300, Frederic Ntirenganya wrote:
> Dear All,
>
> i am solving the following problem in my work.
>
> The first day from April 01 that gets more than 20 mm on a single day, or
> totalled
> over 2 consecutive days. i.e April 01 = 92th day of the year.
>
> The column of interest is "Rain".
> > head(Samaru56)
> Year Day Rain
> 1 1928 1 0
> 2 1928 2 0
> 3 1928 3 0
> 4 1928 4 0
> 5 1928 5 0
> 6 1928 6 0
>
> I used the loop below but it is not printing anything.
>
> sow_day=c()
> for (i in 1928:1983){
> for (j in 92:366){
> k=j-1
> s_rain=Samaru56$Rain[k] + Samaru56$Rain[j]
> if (s_rain>=20)
> sow_day=j
> break
> }
> Samaru56$year=Samaru56$Year[sow_day]
> Samaru56$Day=Samaru56$Day[sow_day]
> Samaru56$Rain=Samaru56$Rain[sow_day]
> }
> sow_day
>
> Any idea is welcome on how I can solve this problem. Thanks
>
More information about the R-help
mailing list