[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:
In `[-.data.frame`(`*tmp*`, order(test$x), , value = list(x = c(2,  :
  provided 3 variables to replace 2 variables.

while the following
***
test$sumy-cumsum(test[order(test$x),]$y)
**
gives
  x y sumy
1 3 84
2 5 1   12
3 2 4   13
4 6 9   22
5 7 0   22

should it gives

  x y sumy
1 3 812
2 5 1   13
3 2 4   4
4 6 9   22
5 7 0   22

What am I missing here?
thanks,
Guang

__
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] 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 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.


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 method.
Really  need to refresh my linear algebra;-)
regards,
cal@cowbo

On 6/14/12, Petr Savicky savi...@cs.cas.cz wrote:
 On Thu, Jun 14, 2012 at 01:11:45PM +, G. Dai wrote:
 Dear Rlisters,
 I'm writing to ask how to manipulate a matrix or dataframe in a specific
 way.

 To elaborate, let's consider an example. Assume we have the following
 3 by 4 matrix A with elements either 0 or 1,
 0  1  1  0
 1  0  1  1
 0  0  0  1

 From the original matrix A, I'd like to generate a new matrix B
 satisfing
 1) B is initially set to be equal to A, and then
 2) Each row of B is a weighted sum of rows of A
 3) The weight is given by the number of common 1 shared by the rows.
 For row 1:
 it has one common 1 with row 2, thus the weight is 1;
 it has zero common 1 with row 3, thus the weight is 0;
 For row 2:
 it has one common 1 with row 1, thus the weight is 1;
 it has one common 1 with row 3, thus the weight is 1;
 For row 3:
 it has zero common 1 with row 1, thus the weight is 0;
 it has one common 1 with row 2, thus the weight is 1;
 4) In this way, the new matrix B is
 1  1  2  1
 1  1  2  2
 1  0  1  2

 Hi.

 If i understand correctly, each row of B is obtained from the
 corresponding row of A by adding mutliples of other rows using
 the weights as described. Try the following.

   a - rbind(
   c(0, 1, 1, 0),
   c(1, 0, 1, 1),
   c(0, 0, 0, 1))

   w - a %*% t(a)
   diag(w) - 1

   w %*% a

[,1] [,2] [,3] [,4]
   [1,]1121
   [2,]1122
   [3,]1012

 The entries of w are the weights except of the diagonal, which
 is all ones, since each row of A contributes to the same row of B
 with weight one. Is this, what you are looking for?

 Petr Savicky.

 __
 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.