Custom Function not recognized by ddply {plyr}, it

2019-09-16 10:28发布

问题:

I have a matrix called (b2) that contains 3565 rows and 125 columns with only dichotomous values (0 and 1)

I designed a function to compare row i and row i+1 and store the number of differences in a new vector.

loopPhudcf <- function(x){
  ## create a vector to store the results of your for loop
  output <- as.vector(rep(0, length(x[,1])))
  for (i in 1:(nrow(x))-1)  {
    output[i]<-as.vector(table(x[i,]==x[i+1,]))[1]
  }
  a<-nrow(x)
  b<-nrow(x)-1
  output<-t(as.matrix(output[c(a,1:b)]))
  output[output==ncol(x)]<-0
  return(output)
}

phudcfily123<-loopPhudcf(b2)

The function works fine, but I also have an ID variable which I added to my original matrix using: b2<-transform(b2,id=a$id), then resulting in a 3565 by 126 being the last one the id variable

I wanted to apply my function using ddply {plyr} but to do this i need to subset only my original matrix without the ID variable (as.matrix(b2[,1:(ncol(b2)-1)])) but it keeps saying that my function is not a function :(

x <- ddply(.data = b2, .var = c("id"), .fun = loopPhudcf(as.matrix(b2[,1:(ncol(b2)-1)])))

Error in llply(.data = .data, .fun = .fun, ..., .progress = .progress,  : 
  .fun is not a function.

can anyone help me overcome this issue?

回答1:

.fun expects just a function; you have given it a function with specific arguments, that is, the result of a function call. You can turn this into an anonymous function.

x <- ddply(.data = b2, .var = c("id"), .fun = function(b2s) {loopPhudcf(as.matrix(b2[,1:(ncol(b2)-1)]))}

I changed to argument of the anonymous function from b2 to b2s; it would have worked without this change, but would have been more confusing because the b2 inside the function would have just been a subset of the original b2.

(Untested because I don't have an example of b2.)



回答2:

Thank you, using the package reshape i was able to get the same result reached using Brian's method, this is the code:

x<-sparseby(as.matrix(b2[,1:125]),list(group = b2[,126]), function(subset) loopPhudcf(as.matrix(b2[,1:125])))

Something a little bit strange to me was that using this approach and the approach kindly suggested by Brian I obtained a new matrix instead of my desired vector

dim(x)
[1]  155 3566

So, I only had to subset the first row to get the vector since the rows contained the same information. My vector with a length of 3565 was obtained using:

x1<-x[1,2:ncol(x)]

I started with 2 given that The first column accounts for the id variable in b2. Thank you again!



标签: r function plyr