Dear All: I have a dataset like A=c(0,12,34,5,6,0,4,5,6,0,12,3,4,8,7,0,4,3,5,0,.......),I want to add a column to this dataset, it must be in B=c(1,1,1,1,1,2,2,2,2,3,3,3,3,3,3,4,4,4,4,5,......), How can I create B based on the sequence of A. Appreciate. Zheng
below works on you example but someone will have something more elegant.
zeroindices<-which(a == 0)
rep(1:length(zeroindices),c(diff(zeroindices),(length(a)-zeroindices[len
gth(zeroindices)]+1)))
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Zheng Lu
Sent: Tuesday, August 28, 2007 5:00 PM
To: r-help at stat.math.ethz.ch
Subject: [R] data manipulation help
Dear All:
I have a dataset like
A=c(0,12,34,5,6,0,4,5,6,0,12,3,4,8,7,0,4,3,5,0,.......),I want to add a
column to this dataset, it must be in
B=c(1,1,1,1,1,2,2,2,2,3,3,3,3,3,3,4,4,4,4,5,......), How can I create B
based on the sequence of A. Appreciate.
Zheng
______________________________________________
R-help at stat.math.ethz.ch 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.
--------------------------------------------------------
This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
On Tue, 28 Aug 2007, Zheng Lu wrote:> Dear All: > > I have a dataset like > A=c(0,12,34,5,6,0,4,5,6,0,12,3,4,8,7,0,4,3,5,0,.......),I want to add a > column to this dataset, it must be in > B=c(1,1,1,1,1,2,2,2,2,3,3,3,3,3,3,4,4,4,4,5,......), How can I create B > based on the sequence of A. Appreciate.Do you want B <- cumsum( A == 0 ) ?? Please use spaces and newlines to make your code more readable!> > > Zheng > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901