[R] the first. from SAS in R

Charles C. Berry cberry at tajo.ucsd.edu
Tue Nov 23 16:57:22 CET 2010


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 secund time 1 appers in a and so on.
>
> so
>
> something similar to:
>
> if first.a {
> a$d<-1
> }else{
> a$d<-0
> }
>
> This would give me
>
>  a b  c b
>  1 1  5 1
>  1 0  2 0
>  2 0  2 1
>  2 0 NA 0
>  2 9  2 0
>  3 1  3 1
>
> Is there such a function in R or anything similar?

See

 	?duplicated

then try

 	a$d <- ifelse( duplicated( a$a ), 0 , 1 )

and

 	a$d.2 <- as.numeric( !duplicated( a$a ) )

HTH,

Chuck

>
>
> thx
>
> //Joel
>
> -- 
> View this message in context: http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3055417.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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                            Dept of Family/Preventive Medicine
cberry at tajo.ucsd.edu			    UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list