powered by
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)
a dataframe with number of rows equal to the number of factor levels in 'new'
a dataframe.
the ids to match the rows in df to the 'new' grouping ids.
the new ids (must be a vector of the same length as 'old'.
the colums to include in the output.
the functions to perform on the output (default is to sum) .
Determines wether to combine with existing groupings or to start fresh.
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)
Run the code above in your browser using DataLab