Hello R User, I have a data set where subject( Id) are frequently measured. For example, the size of the data set is 75 by 2 and has following frequency distribution. id freq 1 30 2 20 3 25 I want to create a variable (say seq) containing sequential count for each id. I mean variable seq should be as ( 1,2,..,30, 1,2,...,20, 1,2,...,25) I use following code but did not work. Any suggestion is much appreciated.. sim<-rep(NA, 90) for (i in 1:3){ d[i]<-seq(1,a[i,2],by=1) sim[i]<-as.vector(c(d[i]) } Thank you, Bibek
> sequence(c(4,2,0,10))[1] 1 2 3 4 1 2 1 2 3 4 5 6 7 8 9 10 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of bibek sharma > Sent: Tuesday, October 09, 2012 11:54 AM > To: r-help at r-project.org > Subject: [R] Qustion about Creating a sequence of vector > > Hello R User, > I have a data set where subject( Id) are frequently measured. For > example, the size of the data set is 75 by 2 and has following > frequency distribution. > > id freq > 1 30 > 2 20 > 3 25 > > I want to create a variable (say seq) containing sequential count for > each id. I mean variable seq should be as ( 1,2,..,30, 1,2,...,20, > 1,2,...,25) > > I use following code but did not work. Any suggestion is much appreciated.. > sim<-rep(NA, 90) > for (i in 1:3){ > d[i]<-seq(1,a[i,2],by=1) > sim[i]<-as.vector(c(d[i]) > } > > Thank you, > Bibek > > ______________________________________________ > 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.
Hi, You can also try this: set.seed(1) dat1<-data.frame(id=rep(c(1,2,3),c(30,20,25)),value=rnorm(75,15)) dat2<-data.frame(stack(lapply(split(dat1,dat1$id),FUN=function(x) 1:nrow(x))),value=dat1[,2]) colnames(dat2)[1:2]<-c("seq1","id") ?dat2$seq1 # [1]? 1? 2? 3? 4? 5? 6? 7? 8? 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #[26] 26 27 28 29 30? 1? 2? 3? 4? 5? 6? 7? 8? 9 10 11 12 13 14 15 16 17 18 19 20 #[51]? 1? 2? 3? 4? 5? 6? 7? 8? 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 A.K. ----- Original Message ----- From: bibek sharma <mbhpathak at gmail.com> To: r-help at r-project.org Cc: Sent: Tuesday, October 9, 2012 2:54 PM Subject: [R] Qustion about Creating a sequence of vector Hello R User, I have a data set where subject( Id)? are frequently measured. For example, the size of the data set is 75 by 2 and has following frequency distribution. id? ? freq 1? ? ? 30 2? ? ? 20 3? ? ? 25 I want to create a variable (say seq) containing sequential count for each id. I mean variable seq should be as ( 1,2,..,30, 1,2,...,20, 1,2,...,25) I use following code but did not work. Any suggestion is much appreciated.. sim<-rep(NA, 90) for (i in 1:3){ d[i]<-seq(1,a[i,2],by=1) sim[i]<-as.vector(c(d[i]) } Thank you, Bibek ______________________________________________ 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.
HI,By customising sequence(), set.seed(1) dat1<-data.frame(id=rep(c(1,2,3),c(30,20,25)),value=rnorm(75,15)) #Either dat1$seq1<-unlist(sapply(table(dat1$id),FUN=function(x) 1:x),use.names=FALSE) #or dat1$seq1<-unlist(sapply(count(dat1$id)$freq,FUN=function(x) seq_len(x))) #can be used. A.K. ----- Original Message ----- From: William Dunlap <wdunlap at tibco.com> To: bibek sharma <mbhpathak at gmail.com>; "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Tuesday, October 9, 2012 3:27 PM Subject: Re: [R] Qustion about Creating a sequence of vector> sequence(c(4,2,0,10))[1]? 1? 2? 3? 4? 1? 2? 1? 2? 3? 4? 5? 6? 7? 8? 9 10 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of bibek sharma > Sent: Tuesday, October 09, 2012 11:54 AM > To: r-help at r-project.org > Subject: [R] Qustion about Creating a sequence of vector > > Hello R User, > I have a data set where subject( Id)? are frequently measured. For > example, the size of the data set is 75 by 2 and has following > frequency distribution. > > id? ? freq > 1? ? ? 30 > 2? ? ? 20 > 3? ? ? 25 > > I want to create a variable (say seq) containing sequential count for > each id. I mean variable seq should be as ( 1,2,..,30, 1,2,...,20, > 1,2,...,25) > > I use following code but did not work. Any suggestion is much appreciated.. > sim<-rep(NA, 90) > for (i in 1:3){ > d[i]<-seq(1,a[i,2],by=1) > sim[i]<-as.vector(c(d[i]) > } > > Thank you, > Bibek > > ______________________________________________ > 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.______________________________________________ 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.