R ddply, applying if and ifelse functions
I'm trying to apply a function to a dataframe using ddply from the plyr package, but I'm getting some results that I don't understand. I have 3 questions about the results Given: mydf<- data.frame(c(12,34,9,3,22,55),c(1,2,1,1,2,2) , c(0,1,2,1,1,2)) colnames(mydf)[1] <- 'n' colnames(mydf)[2] <- 'x' colnames(mydf)[3] <- 'x1' mydf looks like this: n x x1 1 12 1 0 2 34 2 1 3 9 1 2 4 3 1 1 5 22 2 1 6 55 2 2 Question #1 If I do: k <- function(x) { mydf$z <- ifelse(x == 1, 0, mydf$n) return (mydf) } mydf <- ddply(mydf, c("x") , .fun = k, .inform = TRUE) I get the following error: Error in `$<-.data