Nash
2009-Feb-25 12:25 UTC
[R] Have a function like the "_n_" in R ? (Automatic count function )
Have the counter function in R ? if we use the software SAS /*** SAS Code **************************/ data tmp(drop= i); retain seed x 0; do i = 1 to 5; call ranuni(seed,x); output; end; run; data new; counter=_n_; ***** this keyword _n_ ****; set tmp; run; /* _n_ (Automatic variables) are created automatically by the DATA step or by DATA step statements. */ /*** Output ******************************** counter seed x 1 584043288 0.27197 2 935902963 0.43581 3 301879523 0.14057 4 753212598 0.35074 5 1607264573 0.74844 ********************************************/ Have a function like the "_n_" in R ? -- Nash - morrison at ibms.sinica.edu.tw
Henrique Dallazuanna
2009-Feb-25 12:36 UTC
[R] Have a function like the "_n_" in R ? (Automatic count function )
I think you can use seq function: seq(5) On Wed, Feb 25, 2009 at 9:25 AM, Nash <morrison@ibms.sinica.edu.tw> wrote:> > Have the counter function in R ? > > if we use the software SAS > > /*** SAS Code **************************/ > data tmp(drop= i); > retain seed x 0; > do i = 1 to 5; > call ranuni(seed,x); > output; > end; > run; > > data new; > counter=_n_; ***** this keyword _n_ ****; > set tmp; > run; > > /* > _n_ (Automatic variables) > are created automatically by the DATA step or by DATA step statements. > */ > > /*** Output ******************************** > counter seed x > 1 584043288 0.27197 > 2 935902963 0.43581 > 3 301879523 0.14057 > 4 753212598 0.35074 > 5 1607264573 0.74844 > > ********************************************/ > > Have a function like the "_n_" in R ? > > > -- > Nash - morrison@ibms.sinica.edu.tw > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
David Winsemius
2009-Feb-25 14:27 UTC
[R] Have a function like the "_n_" in R ? (Automatic count function )
The _n_ construct in SAS is most analogous to that of row names in R, accessible (and modifiable) via the row.names() function: DF <- structure(list(Month = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L), .Label = c("Aug", "July", "Sept"), class = "factor"), Week = 27:39, Estpassage = c(665L, 2232L, 9241L, 28464L, 41049L, 82216L, 230411L, 358541L, 747839L, 459682L, 609567L, 979475L, 837189L), MedFL = c(34L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 36L, 36L, 36L, 36L)), .Names = c("Month", "Week", "Estpassage", "MedFL"), class = "data.frame", row.names = c(NA, -13L)) DF$counter <- row.names(DF) > DF Month Week Estpassage MedFL counter 1 July 27 665 34 1 2 July 28 2232 35 2 3 July 29 9241 35 3 4 July 30 28464 35 4 5 Aug 31 41049 35 5 6 Aug 32 82216 35 6 7 Aug 33 230411 35 7 8 Aug 34 358541 35 8 9 Sept 35 747839 35 9 10 Sept 36 459682 36 10 11 Sept 37 609567 36 11 12 Sept 38 979475 36 12 13 Sept 39 837189 36 13 Row names, however, not guaranteed to be integer, although if not specified at time of creation a dataframe will have its row names set to an ascending series of integer type. Another function that would provide similar utility for vectors might be seq_along().\, but in the case of dataframes, it may confuse the beginning R user because it will return a column oriented ascending sequence. > seq_along(DF) [1] 1 2 3 4 5 -- David Winsemius On Feb 25, 2009, at 7:25 AM, Nash wrote:> > Have the counter function in R ? > > if we use the software SAS > > /*** SAS Code **************************/ > data tmp(drop= i); > retain seed x 0; > do i = 1 to 5; > call ranuni(seed,x); > output; > end; > run; > > data new; > counter=_n_; ***** this keyword _n_ ****; > set tmp; > run; > > /* > _n_ (Automatic variables) > are created automatically by the DATA step or by DATA step statements. > */ > > /*** Output ******************************** > counter seed x > 1 584043288 0.27197 > 2 935902963 0.43581 > 3 301879523 0.14057 > 4 753212598 0.35074 > 5 1607264573 0.74844 > > ********************************************/ > > Have a function like the "_n_" in R ? > > > -- > Nash - morrison at ibms.sinica.edu.tw > > ______________________________________________ > 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.
justin bem
2009-Feb-25 14:34 UTC
[R] Re : Have a function like the "_n_" in R ? (Automatic count function )
R is more flexible that SAS. You have many functions for loop e.g. for, while, repeat. You also have dim and length functions to get objects dimensions. i<-0 dat<-matrix(c(1, runif(1), .Random.seed[1]),nr=1) repeat{ i=i+1 dat<-rbind(dat, matrix(c(1+i, runif(1), .Random.seed[1]),nr=1)) if (i==4) break } colnames(dat)<-c("counter", "x","seed") dat Justin BEM BP 1917 Yaoundé Tél (237) 99597295 (237) 22040246 ________________________________ De : Nash <morrison@ibms.sinica.edu.tw> À : r-help <r-help@r-project.org> Envoyé le : Mercredi, 25 Février 2009, 13h25mn 18s Objet : [R] Have a function like the "_n_" in R ? (Automatic count function ) Have the counter function in R ? if we use the software SAS /*** SAS Code **************************/ data tmp(drop= i); retain seed x 0; do i = 1 to 5; call ranuni(seed,x); output; end; run; data new; counter=_n_; ***** this keyword _n_ ****; set tmp; run; /* _n_ (Automatic variables) are created automatically by the DATA step or by DATA step statements. */ /*** Output ******************************** counter seed x 1 584043288 0.27197 2 935902963 0.43581 3 301879523 0.14057 4 753212598 0.35074 5 1607264573 0.74844 ********************************************/ Have a function like the "_n_" in R ? -- Nash - morrison@ibms.sinica.edu.tw ______________________________________________ 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. [[alternative HTML version deleted]]
Johannes Hüsing
2009-Feb-26 13:52 UTC
[R] Re : Have a function like the "_n_" in R ? (Automatic count function )
If you are in the context of a data frame (which is closest to the concept of a "data set" in SAS), the 1:nrow(df) is closest to what you may look for. For instance: data(iris) .n. <- 1:nrow(iris) You may notice that this number is not very idiomatic in R. If you have something like: if(_N_ > 50) then output; in R you can simply put iris[-(1:50),] without using an explicit counter variable. In the context of a matrix, the row() and col() functions may do what you want. Am 25.02.2009 um 15:34 schrieb justin bem:> R is more flexible that SAS. You?have many functions for loop e.g. > for, while, repeat. You also have dim and length?functions to get > objects dimensions. > > i<-0 > dat<-matrix(c(1, runif(1), .Random.seed[1]),nr=1) > repeat{ > ??? i=i+1 > ??? dat<-rbind(dat, matrix(c(1+i, runif(1), .Random.seed[1]),nr=1)) > ??? if (i==4) break > } > > colnames(dat)<-c("counter", "x","seed") > dat > > ?Justin BEM > BP 1917 Yaound? > T?l (237) 99597295 > (237) 22040246 > > > > > ________________________________ > De : Nash <morrison at ibms.sinica.edu.tw> > ? : r-help <r-help at r-project.org> > Envoy? le : Mercredi, 25 F?vrier 2009, 13h25mn 18s > Objet?: [R] Have a function like the "_n_" in R ? (Automatic count > function ) > > > Have the counter function in R ? > > if we use the software SAS > > /*** SAS Code **************************/ > data tmp(drop= i); > retain seed x 0; > do i = 1 to 5; > ??? call ranuni(seed,x); > ??? output; > end; > run; > > data new; > counter=_n_;? ***** this keyword _n_ ****; > set tmp; > run; > > /* > _n_ (Automatic variables) > are created automatically by the DATA step or by DATA step statements. > */ > > /*** Output ******************************** > counter??? ? ? seed? ? ? ? ? ? x > 1??? 584043288??? ? ? ? ? 0.27197 > 2??? 935902963??? ? ? ? ? 0.43581 > 3??? 301879523??? ? ? ? ? 0.14057 > 4??? 753212598??? ? ? ? ? 0.35074 > 5??? 1607264573??? 0.74844 > > ********************************************/ > > Have a function like the "_n_" in R ? > > > -- > Nash - morrison at ibms.sinica.edu.tw > > ______________________________________________ > 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. > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.