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?
Thank you,
using the package
reshapei was able to get the same result reached using Brian’s method, this is the code: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
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:
I started with 2 given that The first column accounts for the id variable in b2.
Thank you again!