r - function works perfectly fine outside ddply but throws an error inside ddply -
i trying row-wise operation (by group) on data frame. when run function 1 group, runs fine. however, when put function inside ddply run groups, throws error - argument of length zero.
the function when run separately on data frame 'test':
for (i in 1:(nrow(test) - 5)) { if (i <= 5) { test[i, "mppalert"] <- 0 } firstmpp <- test[i, "tagmppsearchcount"] lastmpp <- test[i+5, "tagmppsearchcount"] if ((lastmpp - firstmpp) >= 10) { test[i+5, "mppalert"] <- 1 } else { test[i+5, "mppalert"] <- 0 } }
the above function inside ddply throws error:
error in if (lastmpp - firstmpp >= 10) { : argument of length 0
below ddply code:
mpp_fn <- function(x) { (i in 1:(nrow(x) - 5)) { if (i <= 5) { x[i, "mppalert"] <- 0 } firstmpp <- x[i, "tagmppsearchcount"] lastmpp <- x[i+5, "tagmppsearchcount"] if (lastmpp - firstmpp >= 10) { x[i+5, "mppalert"] <- 1 } else { x[i+5, "mppalert"] <- 0 } } } result <- ddply(data, c("shelterid", "inverterid"), mpp_fn(x))
in above code, values of firstmpp , lastmpp resolving null , therefore error, why happening (when runs fine outside ddply)?
update: here output of dput(data):
structure(list(shelterid = c("sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh02", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03", "sh03"), inverterid = c("i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i1", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2"), tagmppsearchcount = c(0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 4, 0, 0, 4, 0, 4, 0, 0, 5, 0, 0, 5, 0)), .names = c("shelterid", "inverterid", "tagmppsearchcount" ), row.names = c(350l, 351l, 352l, 353l, 354l, 355l, 356l, 357l, 358l, 359l, 360l, 361l, 362l, 363l, 364l, 365l, 366l, 367l, 494l, 495l, 496l, 497l, 498l, 499l, 500l, 501l, 502l, 503l, 504l, 505l, 506l, 507l, 508l, 509l, 510l, 511l, 638l, 639l, 640l, 641l, 642l, 643l, 644l, 645l, 646l, 647l, 648l, 649l, 650l, 651l, 652l, 653l, 654l, 655l, 782l, 783l, 784l, 785l, 786l, 787l, 788l, 789l, 790l, 791l, 792l, 793l, 794l, 795l, 796l, 797l, 798l, 799l), class = "data.frame")
here dplyr
solution. doesn't require explicit loops
library(dplyr) data %>% group_by(shelterid, inverterid) %>% mutate( first = lag(tagmppsearchcount, 5), mppalert = ifelse( is.na(first), 0, ifelse( tagmppsearchcount - first > 10, 1, 0 ) ) )
Comments
Post a Comment