Skip to content

summing values by week - based on daily dates - but with some dates missing

2 messages · Dimitri Liakhovitski

#
Guys, sorry to bother you again:

I am running everything as before (see code below - before the line
with a lot of ######). But now I am getting an error:
Error in eval(expr, envir, enclos) : could not find function "na.locf"
I also noticed that after I run the 3rd line from the bottom: "wk <-
as.numeric(format(myframe$dates, "%Y.%W"))" - there are some weeks
that end with .00
And then, after I run the 2nd line from the bottom: "is.na(wk) <- wk
%% 1 == 0" those weeks turn into NAs.
Whether I run the second line or not - I get the same error about it
not finding the function "na.locf".
Do you know what might be going on?
Thanks a lot!
Dimitri

### Creating a longer example data set:
mydates<-rep(seq(as.Date("2008-12-29"), length = 500, by = "day"),2)
myfactor<-c(rep("group.1",500),rep("group.2",500))
set.seed(123)
myvalues<-runif(1000,0,1)
myframe<-data.frame(dates=mydates,group=myfactor,value=myvalues)
(myframe)
dim(myframe)

## Removing same rows (dates) unsystematically:
set.seed(123)
removed.group1<-sample(1:500,size=150,replace=F)
set.seed(456)
removed.group2<-sample(501:1000,size=150,replace=F)
to.remove<-c(removed.group1,removed.group2);length(to.remove)
to.remove<-to.remove[order(to.remove)]
myframe<-myframe[-to.remove,]
(myframe)
dim(myframe)
names(myframe)# write.csv(myframe,file="x.test.csv",row.names=F)

wk <- as.numeric(format(myframe$dates, "%Y.%W"))
is.na(wk) <- wk %% 1 == 0
solution<-aggregate(value ~ group + na.locf(wk), myframe, FUN = sum)




###############################################################
On Wed, Mar 30, 2011 at 5:25 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:

  
    
#
Sorry - never mind. It turns out I did not load the zoo package. That
was the reason.

On Wed, Apr 6, 2011 at 12:14 PM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote: