Skip to content

Instantly share code, notes, and snippets.

@berndweiss
Created September 29, 2012 03:42
Show Gist options
  • Select an option

  • Save berndweiss/3803085 to your computer and use it in GitHub Desktop.

Select an option

Save berndweiss/3803085 to your computer and use it in GitHub Desktop.

Revisions

  1. berndweiss revised this gist Oct 9, 2012. 1 changed file with 51 additions and 50 deletions.
    101 changes: 51 additions & 50 deletions by_n.R
    Original file line number Diff line number Diff line change
    @@ -1,50 +1,51 @@
    ## See "Counting with by" for a Stata example
    ## http://www.ats.ucla.edu/stat/stata/notes/countn.htm

    ## Hadley's version (which I like most) using ave() and seq_along()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    mydf
    mydf$v2 <- ave(mydf$v1, mydf$id, FUN = seq_along)
    mydf


    ## 1. Version with table()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    mydf
    mydf <- data.frame(mydf, v2 = as.vector(unlist(lapply(table(mydf$id), seq_len))))
    mydf

    ## > mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    ## > mydf
    ## id v1
    ## 1 1 1
    ## 2 1 1
    ## 3 1 1
    ## 4 2 1
    ## 5 2 1
    ## 6 2 1
    ## 7 2 1
    ## 8 3 1
    ## 9 3 1
    ## 10 3 1
    ## > mydf <- data.frame(mydf, v2 = as.vector(unlist(lapply(table(mydf$id), seq_len))))
    ## > mydf
    ## id v1 v2
    ## 1 1 1 1
    ## 2 1 1 2
    ## 3 1 1 3
    ## 4 2 1 1
    ## 5 2 1 2
    ## 6 2 1 3
    ## 7 2 1 4
    ## 8 3 1 1
    ## 9 3 1 2
    ## 10 3 1 3


    ## 2. Version with by()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    underscore_n <- function(df){n <- dim(df)[1]; seq_len(n)}
    underscore_n(mydf)
    mydf <- data.frame(mydf, v2 = as.vector(unlist(by(mydf, mydf$id, underscore_n))))
    mydf
    ##
    ## See "Counting with by" for a Stata example
    ## http://www.ats.ucla.edu/stat/stata/notes/countn.htm

    ## Hadley's version (which I like most) using ave() and seq_along()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    mydf
    mydf$v2 <- ave(mydf$v1, mydf$id, FUN = seq_along)
    mydf


    ## 1. Version with table()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    mydf
    mydf <- data.frame(mydf, v2 = as.vector(unlist(lapply(table(mydf$id), seq_len))))
    mydf

    ## > mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    ## > mydf
    ## id v1
    ## 1 1 1
    ## 2 1 1
    ## 3 1 1
    ## 4 2 1
    ## 5 2 1
    ## 6 2 1
    ## 7 2 1
    ## 8 3 1
    ## 9 3 1
    ## 10 3 1
    ## > mydf <- data.frame(mydf, v2 = as.vector(unlist(lapply(table(mydf$id), seq_len))))
    ## > mydf
    ## id v1 v2
    ## 1 1 1 1
    ## 2 1 1 2
    ## 3 1 1 3
    ## 4 2 1 1
    ## 5 2 1 2
    ## 6 2 1 3
    ## 7 2 1 4
    ## 8 3 1 1
    ## 9 3 1 2
    ## 10 3 1 3


    ## 2. Version with by()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    underscore_n <- function(df){n <- dim(df)[1]; seq_len(n)}
    underscore_n(mydf)
    mydf <- data.frame(mydf, v2 = as.vector(unlist(by(mydf, mydf$id, underscore_n))))
    mydf
  2. berndweiss revised this gist Oct 5, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion by_n.R
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    ## See "Counting with by" for a Stata example
    ## http://www.ats.ucla.edu/stat/stata/notes/countn.htm

    ## Hadley's version (which I like most) using ave()
    ## Hadley's version (which I like most) using ave() and seq_along()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    mydf
    mydf$v2 <- ave(mydf$v1, mydf$id, FUN = seq_along)
  3. berndweiss revised this gist Oct 5, 2012. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions by_n.R
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,13 @@
    ## See "Counting with by" for a Stata example
    ## http://www.ats.ucla.edu/stat/stata/notes/countn.htm

    ## Hadley's version (which I like most) using ave()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    mydf
    mydf$v2 <- ave(mydf$v1, mydf$id, FUN = seq_along)
    mydf


    ## 1. Version with table()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    mydf
  4. berndweiss revised this gist Sep 30, 2012. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions by_n.R
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,33 @@ mydf
    mydf <- data.frame(mydf, v2 = as.vector(unlist(lapply(table(mydf$id), seq_len))))
    mydf

    ## > mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    ## > mydf
    ## id v1
    ## 1 1 1
    ## 2 1 1
    ## 3 1 1
    ## 4 2 1
    ## 5 2 1
    ## 6 2 1
    ## 7 2 1
    ## 8 3 1
    ## 9 3 1
    ## 10 3 1
    ## > mydf <- data.frame(mydf, v2 = as.vector(unlist(lapply(table(mydf$id), seq_len))))
    ## > mydf
    ## id v1 v2
    ## 1 1 1 1
    ## 2 1 1 2
    ## 3 1 1 3
    ## 4 2 1 1
    ## 5 2 1 2
    ## 6 2 1 3
    ## 7 2 1 4
    ## 8 3 1 1
    ## 9 3 1 2
    ## 10 3 1 3


    ## 2. Version with by()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
  5. berndweiss revised this gist Sep 30, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions by_n.R
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,14 @@
    ## See "Counting with by" for a Stata example
    ## http://www.ats.ucla.edu/stat/stata/notes/countn.htm

    ## 1. Version via table()
    ## 1. Version with table()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    mydf
    mydf <- data.frame(mydf, v2 = as.vector(unlist(lapply(table(mydf$id), seq_len))))
    mydf


    ## 2. Version via by()
    ## 2. Version with by()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    underscore_n <- function(df){n <- dim(df)[1]; seq_len(n)}
    underscore_n(mydf)
  6. berndweiss revised this gist Sep 29, 2012. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions by_n.R
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,16 @@
    ## See "Counting with by" for a Stata example
    ## http://www.ats.ucla.edu/stat/stata/notes/countn.htm

    ## 1. Version via table
    ## 1. Version via table()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    mydf
    mydf <- data.frame(mydf, v2 = as.vector(unlist(lapply(table(mydf$id), seq_len))))
    mydf


    ## 2. Version via
    ## 2. Version via by()
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    .n <- function(df){n <- dim(df)[1]; seq_len(n)}
    .n(mydf)
    as.vector(unlist(by(mydf, mydf$id, .n)))
    underscore_n <- function(df){n <- dim(df)[1]; seq_len(n)}
    underscore_n(mydf)
    mydf <- data.frame(mydf, v2 = as.vector(unlist(by(mydf, mydf$id, underscore_n))))
    mydf
  7. berndweiss revised this gist Sep 29, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions by_n.R
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    ## See "Counting with by" for a Stata example
    ## http://www.ats.ucla.edu/stat/stata/notes/countn.htm

    ## 1. Version via table
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    mydf
  8. berndweiss created this gist Sep 29, 2012.
    12 changes: 12 additions & 0 deletions by_n.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    ## 1. Version via table
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    mydf
    mydf <- data.frame(mydf, v2 = as.vector(unlist(lapply(table(mydf$id), seq_len))))
    mydf


    ## 2. Version via
    mydf <- data.frame(id = c(1,1,1,2,2,2,2,3,3,3), v1 = 1)
    .n <- function(df){n <- dim(df)[1]; seq_len(n)}
    .n(mydf)
    as.vector(unlist(by(mydf, mydf$id, .n)))