is there an equivalent to Stata's egen function?

PAC

Stata has a very nice command, egen, which makes it easy to compute statistics over group of observation. For instance, it is possible to compute the max, the mean and the min for each group and add them as a variable in the detailed data set. The Stata command is one line of code :

by group : egen max = max(x)

I've never found the same command in R. summarise in the dplyr package makes it easy to compute statistics for each group but then I have to run a loop to associate the statistic to each observation :

library("dplyr")
N  <- 1000
tf  <- data.frame(group = sample(1:100, size = N, replace = TRUE), x = rnorm(N))
table(tf$group)
mtf  <- summarise(group_by(tbl_df(tf), group), max = max(x))
tf$max  <- NA
for (i in 1:nrow(mtf)) {
  tf$max[tf$group == mtf$group[i]]  <- mtf$max[i]
}

Does any one has a better solution ?

G. Grothendieck

Here are a few approaches:

dplyr

library(dplyr)

tf %>% group_by(group) %>% mutate(max = max(x))

ave

This uses only the base of R:

transform(tf, max = ave(x, group, FUN = max))

data.table

library(data.table)

dt <- data.table(tf)
dt[, max:=max(x), by=group]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R: Is there an equivalent of Stata's ibn. function?

From Dev

Stata: wildcard with exceptions in egen functions

From Dev

pandas equivalent of Stata's encode

From Dev

Stata: using egen group() to create unique identifiers

From Dev

Stata function equivalent to group_concat

From Dev

What is the equivalent to Stata's portmanteau (Q) test for white noise in R?

From Dev

What is the equivalent to Stata's portmanteau (Q) test for white noise in R?

From Dev

R equivalent of Stata *

From Dev

Arrays / Vector equivalent in Stata

From Dev

Clojure's equivalent to Lisp's atom function

From Dev

Python's equivalent for R's dput() function

From Dev

R equivalent of Stata's for-loop over local macro list of stubnames

From Dev

Is there a Python equivalent to R's sample() function?

From Dev

Equivalent function in JavaScript/Ramda as Clojure's juxt

From Dev

pandas: function equivalent to SQL's datediff()?

From Java

Equivalent of R's factor function in Pandas

From Dev

SQL Server equivalent of Excel's TINV function

From Dev

Python equivalent of R's head and tail function

From Dev

What is the MATLAB equivalent of Excel's NORMSDIST function?

From Dev

what is the Frege equivalent to Haskell's "interact" function?

From Dev

Matlab equivalent of Python's 'reduce' function

From Dev

Is there a Python equivalent to MATLAB's pearsrnd function?

From Dev

What is the equivalent of jQuery's .before() function in Javascript?

From Dev

Is there an equivalent to matlab's rcond() function in Julia?

From Dev

Is there an equivalent to Haskell's init function in elixir?

From Dev

Is there an equivalent to Python's all function in JavaScript or jQuery?

From Dev

What is the Java equivalent to Python's reduce function?

From Dev

Pandas equivalent of Python's readlines function

From Dev

Equivalent of Perl's 'pack' function in Java

Related Related

  1. 1

    R: Is there an equivalent of Stata's ibn. function?

  2. 2

    Stata: wildcard with exceptions in egen functions

  3. 3

    pandas equivalent of Stata's encode

  4. 4

    Stata: using egen group() to create unique identifiers

  5. 5

    Stata function equivalent to group_concat

  6. 6

    What is the equivalent to Stata's portmanteau (Q) test for white noise in R?

  7. 7

    What is the equivalent to Stata's portmanteau (Q) test for white noise in R?

  8. 8

    R equivalent of Stata *

  9. 9

    Arrays / Vector equivalent in Stata

  10. 10

    Clojure's equivalent to Lisp's atom function

  11. 11

    Python's equivalent for R's dput() function

  12. 12

    R equivalent of Stata's for-loop over local macro list of stubnames

  13. 13

    Is there a Python equivalent to R's sample() function?

  14. 14

    Equivalent function in JavaScript/Ramda as Clojure's juxt

  15. 15

    pandas: function equivalent to SQL's datediff()?

  16. 16

    Equivalent of R's factor function in Pandas

  17. 17

    SQL Server equivalent of Excel's TINV function

  18. 18

    Python equivalent of R's head and tail function

  19. 19

    What is the MATLAB equivalent of Excel's NORMSDIST function?

  20. 20

    what is the Frege equivalent to Haskell's "interact" function?

  21. 21

    Matlab equivalent of Python's 'reduce' function

  22. 22

    Is there a Python equivalent to MATLAB's pearsrnd function?

  23. 23

    What is the equivalent of jQuery's .before() function in Javascript?

  24. 24

    Is there an equivalent to matlab's rcond() function in Julia?

  25. 25

    Is there an equivalent to Haskell's init function in elixir?

  26. 26

    Is there an equivalent to Python's all function in JavaScript or jQuery?

  27. 27

    What is the Java equivalent to Python's reduce function?

  28. 28

    Pandas equivalent of Python's readlines function

  29. 29

    Equivalent of Perl's 'pack' function in Java

HotTag

Archive