regroup | R Documentation |
Used to group a dataframe of numbers by a factor that need not be the same length. Find the a factor in the old df and use it to group by the new trumping factor (NA's allowed)
regroup(df, old, new, clmns, funcs=rep('sum',length(clmns)), combine=TRUE)
df |
a dataframe. |
old |
the ids to match the rows in df to the 'new' grouping ids. |
new |
the new ids (must be a vector of the same length as 'old'. |
clmns |
the colums to include in the output. |
funcs |
the functions to perform on the output (default is to sum) . |
combine |
Determines wether to combine with existing groupings or to start fresh. |
a dataframe with number of rows equal to the number of factor levels in 'new'
df <- data.frame(a=rnorm(20),b=rpois(20,1))
mapping <- data.frame(old=rownames(df), new=rep(c('a','b'),10))
regroup(df, old=mapping$old, new=mapping$new)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.