Re: [R] the first. from SAS in R

2010-11-29 Thread Nutter, Benjamin
Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of David Katz Sent: Wednesday, November 24, 2010 10:17 AM To: r-help@r-project.org Subject: Re: [R] the first. from SAS in R Often the purpose of first/last in sas is to facilitate grouping

Re: [R] the first. from SAS in R

2010-11-24 Thread David Katz
Often the purpose of first/last in sas is to facilitate grouping of observations in a sequential algorithm. This purpose is better served in R by using vectorized methods like those in package plyr. Also, note that first/last has different meanings in the context of "by x;" versus "by x notsorted

Re: [R] the first. from SAS in R

2010-11-23 Thread William Dunlap
Behalf Of > seeliger.c...@epamail.epa.gov > Sent: Tuesday, November 23, 2010 10:38 AM > To: r-help@r-project.org > Cc: Joel > Subject: Re: [R] the first. from SAS in R > > > > Is there any similar function in R to the first. in SAS? > > ?duplicated &

Re: [R] the first. from SAS in R

2010-11-23 Thread Seeliger . Curt
> > ... > > # I've found I need to lag a column to mimic SAS' first. > > # operator, thusly, though perhaps someone else knows > > # differently. Note this does not work on unordered > > # dataframes! > > lag.k1 <- c(NA, tt$k1[1:(nrow(tt) - 1)]) > > tt$r.first.k1 <- ifelse(is.na(lag.k1), 1, tt$k1

Re: [R] the first. from SAS in R

2010-11-23 Thread Charles C. Berry
On Tue, 23 Nov 2010, seeliger.c...@epamail.epa.gov wrote: Is there any similar function in R to the first. in SAS? ?duplicated a$d <- ifelse( duplicated( a$a ), 0 , 1 ) a$d.2 <- as.numeric( !duplicated( a$a ) ) Actually, duplicated does not duplicate SAS' first. operator, though

Re: [R] the first. from SAS in R

2010-11-23 Thread Seeliger . Curt
> > Is there any similar function in R to the first. in SAS? > ?duplicated > > a$d <- ifelse( duplicated( a$a ), 0 , 1 ) > > a$d.2 <- as.numeric( !duplicated( a$a ) ) Actually, duplicated does not duplicate SAS' first. operator, though it may suffice for the OP's needs. To illustra

Re: [R] the first. from SAS in R

2010-11-23 Thread Charles C. Berry
On Tue, 23 Nov 2010, David Winsemius wrote: On Nov 23, 2010, at 11:04 AM, Charles C. Berry wrote: On Tue, 23 Nov 2010, Dennis Murphy wrote: > Interesting. Check this out: > > u <- sample(c(TRUE, FALSE), 10, replace = TRUE) > > u > [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FA

Re: [R] the first. from SAS in R

2010-11-23 Thread David Winsemius
On Nov 23, 2010, at 11:04 AM, Charles C. Berry wrote: On Tue, 23 Nov 2010, Dennis Murphy wrote: Interesting. Check this out: u <- sample(c(TRUE, FALSE), 10, replace = TRUE) u [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE class(u) [1] "logical" u + 0 [1] 0 0 1 0 0 1 0 0

Re: [R] the first. from SAS in R

2010-11-23 Thread David L Lorenz
It all has to do with the precedence of the ! operator. Compare !duplicated(v) + 0 with (!duplicated(v)) + 0 Dave From: "Charles C. Berry" To: Dennis Murphy Cc: r-help@r-project.org Date: 11/23/2010 10:08 AM Subject: Re: [R] the first. from SAS in R Sent by: r-help-boun...@r-p

Re: [R] the first. from SAS in R

2010-11-23 Thread Charles C. Berry
On Tue, 23 Nov 2010, Dennis Murphy wrote: Interesting. Check this out: u <- sample(c(TRUE, FALSE), 10, replace = TRUE) u [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE class(u) [1] "logical" u + 0 [1] 0 0 1 0 0 1 0 0 0 0 0 + u [1] 0 0 1 0 0 1 0 0 0 0 v <- rpois(10, 3)

Re: [R] the first. from SAS in R

2010-11-23 Thread Charles C. Berry
On Tue, 23 Nov 2010, Joel wrote: Is there any similar function in R to the first. in SAS? What it dose is: Lets say we have this table: a b c 1 1 5 1 0 2 2 0 2 2 0 NA 2 9 2 3 1 3 and then I want do to do one thing the first time the number 1 appers in a and something else the

Re: [R] the first. from SAS in R

2010-11-23 Thread Dennis Murphy
Interesting. Check this out: u <- sample(c(TRUE, FALSE), 10, replace = TRUE) > u [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE > class(u) [1] "logical" > u + 0 [1] 0 0 1 0 0 1 0 0 0 0 > 0 + u [1] 0 0 1 0 0 1 0 0 0 0 v <- rpois(10, 3) > !duplicated(v) [1] TRUE FALSE TRUE T

Re: [R] the first. from SAS in R

2010-11-23 Thread David Winsemius
On Nov 23, 2010, at 8:33 AM, Joel wrote: Is there any similar function in R to the first. in SAS? What it dose is: Lets say we have this table: a b c 1 1 5 1 0 2 2 0 2 2 0 NA 2 9 2 3 1 3 and then I want do to do one thing the first time the number 1 appers in a and somethi

Re: [R] the first. from SAS in R

2010-11-23 Thread Gustavo Carvalho
Perhaps something like this: a$d <- ifelse(duplicated(a$a), 0, 1) On Tue, Nov 23, 2010 at 1:33 PM, Joel wrote: > > Is there any similar function in R to the first. in SAS? > > What it dose is: > > Lets say we have this table: > >  a b  c >  1 1  5 >  1 0  2 >  2 0  2 >  2 0 NA >  2 9  2 >  3 1  

[R] the first. from SAS in R

2010-11-23 Thread Joel
Is there any similar function in R to the first. in SAS? What it dose is: Lets say we have this table: a b c 1 1 5 1 0 2 2 0 2 2 0 NA 2 9 2 3 1 3 and then I want do to do one thing the first time the number 1 appers in a and something else the secund time 1 appers in a and