Hello. I am trying  to work out some complicated if() logic.
I thought of using which() and if() but cannot get it.

I have a dataframe that looks like this:

head(deleteFridayTest)

       Date nrec

1 2011-07-17  667

2 2011-07-18  266

3 2009-10-29   29

4 2009-10-30  211

5 2009-10-31  237

6 2009-11-01  898

I want to take the values in nrec for consecutive Friday, Saturday and
Sundays and average them and replace Sundays value with that average.

I came up with this:

deleteFridayTest[dayOfWeek(deleteFridayTest$Date)=="Sun",]$nrec <-
(deleteFridayTest[dayOfWeek(deleteFridayTest$Date)=="Sun",]$nrec +
deleteFridayTest[dayOfWeek(deleteFridayTest$Date)=="Sat",]$nrec +
deleteFridayTest[dayOfWeek(deleteFridayTest$Date)=="Fri",]$nrec)/3

but this won't work for my data because sometimes one or more of the days
of data may be missing. For example Friday's data could be missing, or
Friday and Saturday, or Sunday may be missing, or they all may be missing,
etc.

The rule I want to implement is that
if any of Friday, Saturday, or Sunday is available then I want to have an
entry for Sunday (call it 'X'). If all 3 days are missing then nothing
should be done and there will be no entry for X. If any of the days Fri,
Sat, Sun are available then X should be the "average" of those values (e.g.
if two days are available then sum and divide by 2, if just one day is
available then just use that value for X).

Can anyone suggest how to go about this?
 Thank you.

        [[alternative HTML version deleted]]

______________________________________________
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
and provide commented, minimal, self-contained, reproducible code.

Reply via email to