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
Have a function like the "_n_" in R ? (Automatic count function )
12 messages · Nash, Henrique Dallazuanna, David Winsemius +6 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090225/206d90ad/attachment-0001.pl>
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.
And for completeness here's a function that returns the next integer
on each call.
n <- (function(){
i <- 0
function() {
i <<- i + 1
i
}
})()
n()
[1] 1
n()
[1] 2
n()
[1] 3
n()
[1] 4
n()
[1] 5
n()
[1] 6 ;) Hadley
On Wed, Feb 25, 2009 at 8:27 AM, David Winsemius <dwinsemius at comcast.net> wrote:
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.
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090225/2def806b/attachment-0001.pl>
hadley wickham wrote:
And for completeness here's a function that returns the next integer
on each call.
n <- (function(){
i <- 0
function() {
i <<- i + 1
i
}
})()
actually, you do not need the external function to have the functionality:
n = local({
i = 0
function() (i <<- i + 1)[1] })
n()
# 1
n()
# 2
vQ
On Wed, Feb 25, 2009 at 8:41 AM, Wacek Kusnierczyk
<Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
hadley wickham wrote:
And for completeness here's a function that returns the next integer
on each call.
n <- (function(){
? i <- 0
? function() {
? ? i <<- i + 1
? ? i
? }
})()
actually, you do not need the external function to have the functionality:
? ?n = local({
? ? ? ?i = 0
? ? ? ?function() (i <<- i + 1)[1] })
? ?n()
? ?# 1
? ?n()
? ?# 2
Yes, I'm just using the function as a way of creating an environment. The function method is a little more flexible if you want multiple independent counters though. Hadley
On Wed, Feb 25, 2009 at 3:30 PM, hadley wickham <h.wickham at gmail.com> wrote:
And for completeness here's a function that returns the next integer
on each call.
n <- (function(){
?i <- 0
?function() {
? ?i <<- i + 1
? ?i
?}
})()
n()
[1] 1
n()
[1] 2
n()
[1] 3
n()
[1] 4
n()
[1] 5
n()
[1] 6 ;) Hadley
*headache*! I can't wrap my head around this one - too strange code! Could someone please give a hint on what's going on? How does"i<<- i+1" modify i permanently, seeing as i is defined as 0 to start with? /Gustaf
Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik
On Wed, 25 Feb 2009, Gustaf Rydevik wrote:
On Wed, Feb 25, 2009 at 3:30 PM, hadley wickham <h.wickham at gmail.com> wrote:
And for completeness here's a function that returns the next integer
on each call.
n <- (function(){
?i <- 0
?function() {
? ?i <<- i + 1
? ?i
?}
})()
n()
[1] 1
n()
[1] 2
n()
[1] 3
n()
[1] 4
n()
[1] 5
n()
[1] 6 ;) Hadley
*headache*! I can't wrap my head around this one - too strange code! Could someone please give a hint on what's going on? How does"i<<- i+1" modify i permanently, seeing as i is defined as 0 to start with?
i is not _defined_ as zero. It is initially _assigned_ the value of zero and is subsequently assigned other values. As for the details of what goes here, see An Introduction to R Section 10.7 Scope and study the open.acount() example there. HTH, Chuck
/Gustaf -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik
______________________________________________ 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.
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
hadley wickham wrote:
On Wed, Feb 25, 2009 at 8:41 AM, Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
hadley wickham wrote:
And for completeness here's a function that returns the next integer
on each call.
n <- (function(){
i <- 0
function() {
i <<- i + 1
i
}
})()
actually, you do not need the external function to have the functionality:
n = local({
i = 0
function() (i <<- i + 1)[1] })
n()
# 1
n()
# 2
Yes, I'm just using the function as a way of creating an environment. The function method is a little more flexible if you want multiple independent counters though.
not as it stands above, because you immediately apply your function and
lose grip of it -- so it's just as do-once a solution as that with
local. but clearly, to have multiple independent counters, you'd need
two nested functions, as in this generalized version:
make.counter =
function(value)
function(increment)
(value <<- value + increment)[1]
counters =
lapply(rep(0, 3), make.counter)
mapply(
function(counter, increment)
counter(increment),
counters, 1)
# 1 1 1
which is what you presumably had in mind, roughly.
vQ
On Wed, Feb 25, 2009 at 4:43 PM, Charles C. Berry <cberry at tajo.ucsd.edu> wrote:
On Wed, 25 Feb 2009, Gustaf Rydevik wrote:
On Wed, Feb 25, 2009 at 3:30 PM, hadley wickham <h.wickham at gmail.com> wrote:
And for completeness here's a function that returns the next integer
on each call.
n <- (function(){
?i <- 0
?function() {
? ?i <<- i + 1
? ?i
?}
})()
n()
[1] 1
n()
[1] 2
n()
[1] 3
n()
[1] 4
n()
[1] 5
n()
[1] 6 ;) Hadley
*headache*! I can't wrap my head around this one - too strange code! Could someone please give a hint on what's going on? How does"i<<- i+1" modify i permanently, seeing as i is defined as 0 to start with?
i is not _defined_ as zero. It is initially _assigned_ the value of zero and is subsequently assigned other values. As for the details of what goes here, see ? ? ? ?An Introduction to R ? ? ? ?Section 10.7 Scope and study the open.acount() ?example there. HTH, Chuck
Thank you - I think I finally understood how that code got parsed.
Does the text below describe things correctly?
First, Hadley defines a function that returns another function, like this:
function(){
i <- 0
function() {
i <<- i + 1
i
}
}
Since the returned function is defined in a local environment , R
returns the function together with that local environment, and lexical
scoping can work it's magic
Finally Hadley evaluates the above defined function-returning
function, and stores the returned function in n.
n<-function(){
i <- 0
function() {
i <<- i + 1
i
}
}()
*Phew*
That wasn't too difficult after all :-)
/Gustaf
Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik
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.