[R] Re : Have a function like the "_n_" in R ? (Automatic count function )

Johannes Hüsing johannes at huesing.name
Thu Feb 26 14:52:51 CET 2009


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.




More information about the R-help mailing list