For some reason I cannot use a custom function with ddply. It returns exactly the same data frame.
Basically, I want not to count the number of duplicates of id, but actually create a variable that says if it is the first, second, or third instance of that repetition of id. Wrote a function for that , create_guide, which works; but does not work with the id groups.
df<-data.frame(id=c(1,1,2,2,3,4))
create_guide <- function(dt) {
guide <- rep(0,times=nrow(dt))
for (i in 1:nrow(dt)) {
guide[i] <- length(dt[1:i,1])
}
a <- cbind(guide,dt)
}
bi <- plyr::ddply(df,.(id),fun=create_guide)
What is happening? Thank you
iin the first expression without having defined it previously. Whatiis it using?.fun=, notfun=.