[R] why order doesn't work?

2012-07-27 Thread cowboy
hi all, I want to get a cumsum according to the order of some variable. However, it doesnt' work. For example, ** test<-data.frame(cbind(x=c(3,5,2,6,7),y=c(8,1,4,9,0))) test[order(test$x),]$sumy<-cumsum(test[order(test$x),]$y) ** R complians Warning message:

[R] how to use by function

2012-06-18 Thread cowboy
hi all, Assume I have data like data<-rbind(c(1,2),c(1,3),c(2,1),c(3,2),c(3,4)) I want to get some matrix like 1,2,3 2,NA,NA 3,2,4 I'm using by mat<-matrix(NA,3,3) by(data,data[,1],mat[data[,1],]<-c(data[,2])) but it doesn't work. Any id

Re: [R] matrix manipulation

2012-06-14 Thread cowboy
thank you, Petr. This is exactly what I'm looking for in my post. An related question can be how to get an arbitrary weight, say if row1 and row 2 have 1 common value 1, then assign a weight 10, if row 1 and row 2 have 2 common value 1, then assign a weight 12. I'm not so sure how to expand your me