[R] Equivalent to Stata egen tag

David Winsemius dwinsemius at comcast.net
Sun Jan 23 23:37:17 CET 2011


On Jan 23, 2011, at 2:07 PM, Denis Kazakevich wrote:

> Dear David
> Thank you for your e-mail
> I believe that  equivalent to Stata egen tag is simply
>
> df$tag <- df[!duplicated(df[c("station","week")]),]
>
> I am not sure about
> egen count
>
> It might be something like
> aggregate(station~week, df, function(x)sum(!is.na(x)), na.action =  
> na.pass)
>
>
> I made this question while making very first steps in R after  
> actively using
> Stata for 3 years. And I underestimated  the extent of flexibility  
> of R. But
> after your answer on one of my previous questions I realized that I  
> can  use,
> for instance mean(tapply(<sth>)) or table(<sth>)/<sth> right away  
> which is great
>

As I was reading I wondered if the "count" solution might be something  
like:

tapply(df$item , df$category, length)

or for a condition:

tapply(df$item , factor( df$variable > const) , length)

Which gives you similar applicability as table


>
> With best regards
> Denis
>
>
>
>
> ----- Original Message ----
> From: David Winsemius <dwinsemius at comcast.net>
> To: Denis Kazakevich <den2042 at yahoo.com>
> Sent: Sun, 23 January, 2011 17:56:53
> Subject: Re: [R]  Equivalent to Stata egen tag
>
>
> On Jan 16, 2011, at 6:48 PM, Denis Kazakevich wrote:
>
>> What are the R equivalents to the Stata command egen tag
>> and
>> egen count?
>>
>> egen station_week_tag = tag(station week)
>
> I have looked at similar questions in the past and gone to the Stata  
> online docs
> and tried to make some sense of them and even posted an attempt at  
> an answer.
> The documents appeared to be to be fairly cryptic. Much more so that  
> the R
> documentation. I was somewhat snottily informed by experienced Stata  
> users that
> I had completely misunderstood the documentation and that egen was  
> much more
> rich an varied that I understood. So I've given answering Stata  
> questions. Given
> the protean nature of the egen I think it's unrealistic of you to  
> expect that
> just posting code will communicate what you intended.
>
>
> --
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
>
>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list