Here is a gist of what I want to do:
I've got 2 data frames:
x (id is unique)
id timestamp
282462839 2012-12-05 10:55:00
282462992 2012-12-05 12:08:00
282462740 2012-12-05 12:13:00
282462999 2012-12-05 12:48:00
y (id is not unique)
id value1 value2
282462839 300 100
282462839 300 200
282462839 400 300
282462999 500 400
282462999 300 150
I also have a function myfunc(id,pvalue) that computes something and returns one of the value2 values depending on pvalue and other value1s (more complicated than just pvalue==value1)
I want to create a 3rd column for x that contains the corresponding computed myfunc(id,pvalue), where pvalue is an integer that is constant(say 20).
so in essence, I want to do this:
x$t20 <- myfunc(x$id,20)
I tried using lappy and sapply this way:
x$t20 <- sapply(as.vector(x$id),myfunc,pvalue=20)
I tried using lapply and without the as.vector as well, but I kept getting this error:
Error in .pointsToMatrix(p2) : Wrong length for a vector, should be 2
It works when I just give mean where it just replicates $id in $t20.
How do I do this?
EDIT 1: Here's a skeleton of myfunc:
myfunc <- function(xid,pvalue) {
result <- subset(y,id==xid)
retVal <- -1
if(nrow(result) < 12){
return(NaN)
}
for(i in (1:nrow(result))){
#code to process result
}
return(retVal)
}