Created
September 29, 2012 03:42
-
-
Save berndweiss/3803085 to your computer and use it in GitHub Desktop.
Revisions
-
berndweiss revised this gist
Oct 9, 2012 . 1 changed file with 51 additions and 50 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
berndweiss revised this gist
Oct 5, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() 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) -
berndweiss revised this gist
Oct 5, 2012 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
berndweiss revised this gist
Sep 30, 2012 . 1 changed file with 27 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) -
berndweiss revised this gist
Sep 30, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 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) -
berndweiss revised this gist
Sep 29, 2012 . 1 changed file with 6 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() 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() 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 -
berndweiss revised this gist
Sep 29, 2012 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
berndweiss created this gist
Sep 29, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)))