[R] why order doesn't work?
arun
smartpink111 at yahoo.com
Fri Jul 27 23:04:49 CEST 2012
HI,
I guess you want to get the cumsum of y according to the order of x,
test1<-test[order(test$x),]
test2<-within(test1,{cumsumy<-cumsum(y)})
test2
# x y cumsumy
#3 2 4 4
#1 3 8 12
#2 5 1 13
#4 6 9 22
A#5 7 0 22
A.K.
----- Original Message -----
From: cowboy <dgecon at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Friday, July 27, 2012 4:22 PM
Subject: [R] why order doesn't work?
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 8 4
2 5 1 12
3 2 4 13
4 6 9 22
5 7 0 22
should it gives
x y sumy
1 3 8 12
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 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