Nguyen Dinh Nguyen
2007-Apr-02 02:00 UTC
[R] Create a new var reflecting the order of subjects in existing var
Dear R helpers I have a data set sth like this: set.seed(123);dat <- data.frame(ID= c(rep(1,2),rep(2,3), rep(3,3), rep(4,4), rep(5,5)), var1 =rnorm(17, 35,2), var2=runif(17,0,1)) dat ID var1 var2 1 1 33.87905 0.02461368 2 1 34.53965 0.47779597 3 2 38.11742 0.75845954 4 2 35.14102 0.21640794 5 2 35.25858 0.31818101 6 3 38.43013 0.23162579 7 3 35.92183 0.14280002 8 3 32.46988 0.41454634 9 4 33.62629 0.41372433 10 4 34.10868 0.36884545 11 4 37.44816 0.15244475 12 4 35.71963 0.13880606 13 5 35.80154 0.23303410 14 5 35.22137 0.46596245 15 5 33.88832 0.26597264 16 5 38.57383 0.85782772 17 5 35.99570 0.04583117 I would like to create a new var in dat which reflects the order of each subject (ID), like this ID var1 var2 IDorder 1 1 33.87905 0.02461368 1 2 1 34.53965 0.47779597 2 3 2 38.11742 0.75845954 1 4 2 35.14102 0.21640794 2 5 2 35.25858 0.31818101 3 6 3 38.43013 0.23162579 1 7 3 35.92183 0.14280002 2 8 3 32.46988 0.41454634 3 9 4 33.62629 0.41372433 1 10 4 34.10868 0.36884545 2 11 4 37.44816 0.15244475 3 12 4 35.71963 0.13880606 4 13 5 35.80154 0.23303410 1 14 5 35.22137 0.46596245 2 15 5 33.88832 0.26597264 3 16 5 38.57383 0.85782772 4 17 5 35.99570 0.04583117 5 Thank you very much for your help Regards Nguyen
Christos Hatzis
2007-Apr-02 02:10 UTC
[R] Create a new var reflecting the order of subjects in existingvar
Try this: y <- rle(dat$ID) unlist(sapply(y$lengths, FUN=function(x) seq(1,x))) Christos Hatzis, Ph.D. Nuvera Biosciences, Inc. 400 West Cummings Park Suite 5350 Woburn, MA 01801 Tel: 781-938-3830 www.nuverabio.com> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Nguyen > Dinh Nguyen > Sent: Sunday, April 01, 2007 10:00 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Create a new var reflecting the order of > subjects in existingvar > > Dear R helpers > I have a data set sth like this: > set.seed(123);dat <- data.frame(ID= c(rep(1,2),rep(2,3), > rep(3,3), rep(4,4), rep(5,5)), > var1 =rnorm(17, 35,2), > var2=runif(17,0,1)) > dat > ID var1 var2 > 1 1 33.87905 0.02461368 > 2 1 34.53965 0.47779597 > 3 2 38.11742 0.75845954 > 4 2 35.14102 0.21640794 > 5 2 35.25858 0.31818101 > 6 3 38.43013 0.23162579 > 7 3 35.92183 0.14280002 > 8 3 32.46988 0.41454634 > 9 4 33.62629 0.41372433 > 10 4 34.10868 0.36884545 > 11 4 37.44816 0.15244475 > 12 4 35.71963 0.13880606 > 13 5 35.80154 0.23303410 > 14 5 35.22137 0.46596245 > 15 5 33.88832 0.26597264 > 16 5 38.57383 0.85782772 > 17 5 35.99570 0.04583117 > I would like to create a new var in dat which reflects the > order of each subject (ID), like this > ID var1 var2 IDorder > 1 1 33.87905 0.02461368 1 > 2 1 34.53965 0.47779597 2 > 3 2 38.11742 0.75845954 1 > 4 2 35.14102 0.21640794 2 > 5 2 35.25858 0.31818101 3 > 6 3 38.43013 0.23162579 1 > 7 3 35.92183 0.14280002 2 > 8 3 32.46988 0.41454634 3 > 9 4 33.62629 0.41372433 1 > 10 4 34.10868 0.36884545 2 > 11 4 37.44816 0.15244475 3 > 12 4 35.71963 0.13880606 4 > 13 5 35.80154 0.23303410 1 > 14 5 35.22137 0.46596245 2 > 15 5 33.88832 0.26597264 3 > 16 5 38.57383 0.85782772 4 > 17 5 35.99570 0.04583117 5 > > Thank you very much for your help > Regards > Nguyen > >
jim holtman
2007-Apr-02 02:18 UTC
[R] Create a new var reflecting the order of subjects in existing var
set.seed(123);dat <- data.frame(ID= c(rep(1,2),rep(2,3), rep(3,3), rep(4,4), rep(5,5)), var1 =rnorm(17, 35,2), var2=runif(17,0,1)) dat$order <- ave(dat$ID, dat$ID, FUN=seq) On 4/1/07, Nguyen Dinh Nguyen <n.nguyen@garvan.org.au> wrote:> > Dear R helpers > I have a data set sth like this: > set.seed(123);dat <- data.frame(ID= c(rep(1,2),rep(2,3), rep(3,3), > rep(4,4), > rep(5,5)), > var1 =rnorm(17, 35,2), > var2=runif(17,0,1)) > dat > ID var1 var2 > 1 1 33.87905 0.02461368 > 2 1 34.53965 0.47779597 > 3 2 38.11742 0.75845954 > 4 2 35.14102 0.21640794 > 5 2 35.25858 0.31818101 > 6 3 38.43013 0.23162579 > 7 3 35.92183 0.14280002 > 8 3 32.46988 0.41454634 > 9 4 33.62629 0.41372433 > 10 4 34.10868 0.36884545 > 11 4 37.44816 0.15244475 > 12 4 35.71963 0.13880606 > 13 5 35.80154 0.23303410 > 14 5 35.22137 0.46596245 > 15 5 33.88832 0.26597264 > 16 5 38.57383 0.85782772 > 17 5 35.99570 0.04583117 > I would like to create a new var in dat which reflects the order of each > subject (ID), like this > ID var1 var2 IDorder > 1 1 33.87905 0.02461368 1 > 2 1 34.53965 0.47779597 2 > 3 2 38.11742 0.75845954 1 > 4 2 35.14102 0.21640794 2 > 5 2 35.25858 0.31818101 3 > 6 3 38.43013 0.23162579 1 > 7 3 35.92183 0.14280002 2 > 8 3 32.46988 0.41454634 3 > 9 4 33.62629 0.41372433 1 > 10 4 34.10868 0.36884545 2 > 11 4 37.44816 0.15244475 3 > 12 4 35.71963 0.13880606 4 > 13 5 35.80154 0.23303410 1 > 14 5 35.22137 0.46596245 2 > 15 5 33.88832 0.26597264 3 > 16 5 38.57383 0.85782772 4 > 17 5 35.99570 0.04583117 5 > > Thank you very much for your help > Regards > Nguyen > > > ______________________________________________ > R-help@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. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
Gabor Grothendieck
2007-Apr-02 04:27 UTC
[R] Create a new var reflecting the order of subjects in existing var
Assuming the ID's are contiguous, as in your example: transform(dat, IDorder = seq(ID) - match(ID, ID) + 1) On 4/1/07, Nguyen Dinh Nguyen <n.nguyen at garvan.org.au> wrote:> Dear R helpers > I have a data set sth like this: > set.seed(123);dat <- data.frame(ID= c(rep(1,2),rep(2,3), rep(3,3), rep(4,4), > rep(5,5)), > var1 =rnorm(17, 35,2), > var2=runif(17,0,1)) > dat > ID var1 var2 > 1 1 33.87905 0.02461368 > 2 1 34.53965 0.47779597 > 3 2 38.11742 0.75845954 > 4 2 35.14102 0.21640794 > 5 2 35.25858 0.31818101 > 6 3 38.43013 0.23162579 > 7 3 35.92183 0.14280002 > 8 3 32.46988 0.41454634 > 9 4 33.62629 0.41372433 > 10 4 34.10868 0.36884545 > 11 4 37.44816 0.15244475 > 12 4 35.71963 0.13880606 > 13 5 35.80154 0.23303410 > 14 5 35.22137 0.46596245 > 15 5 33.88832 0.26597264 > 16 5 38.57383 0.85782772 > 17 5 35.99570 0.04583117 > I would like to create a new var in dat which reflects the order of each > subject (ID), like this > ID var1 var2 IDorder > 1 1 33.87905 0.02461368 1 > 2 1 34.53965 0.47779597 2 > 3 2 38.11742 0.75845954 1 > 4 2 35.14102 0.21640794 2 > 5 2 35.25858 0.31818101 3 > 6 3 38.43013 0.23162579 1 > 7 3 35.92183 0.14280002 2 > 8 3 32.46988 0.41454634 3 > 9 4 33.62629 0.41372433 1 > 10 4 34.10868 0.36884545 2 > 11 4 37.44816 0.15244475 3 > 12 4 35.71963 0.13880606 4 > 13 5 35.80154 0.23303410 1 > 14 5 35.22137 0.46596245 2 > 15 5 33.88832 0.26597264 3 > 16 5 38.57383 0.85782772 4 > 17 5 35.99570 0.04583117 5 > > Thank you very much for your help > Regards > Nguyen > > > ______________________________________________ > 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. > >