Hi, Not familiar with by function. But you can get the result :
mat[,1]<-(rle(data[,1]))$values mat[1,]<-(rle(data[,1]))$values mat[3,2:3]<-(rle(data[,2])$values)[4:5] > mat [,1] [,2] [,3] [1,] 1 2 3 [2,] 2 NA NA [3,] 3 2 4 A.K. ----- Original Message ----- From: cowboy <dge...@gmail.com> To: r-help@r-project.org Cc: Sent: Monday, June 18, 2012 11:32 PM Subject: [R] how to use by function 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 ideas? thanks, cowboy ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.