[R] how to use by function

arun smartpink111 at yahoo.com
Tue Jun 19 07:26:12 CEST 2012


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 <dgecon at gmail.com>
To: r-help at 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 at 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.




More information about the R-help mailing list