I am wondering what would be the best way to add, for example, columns of quantiles to the dataset. I was thinking to use ave() function for that, something like ave(iris$Sepal.Length, iris$Species, FUN=quantile) – but in this case ave() merges values returned by quantile() (which in this case returns 5 values per subset) and cut them for the length of iris…
Thanks in advance for suggestions!
There are a lot of SO questions on this general topic, recommending various uses of
ave(),aggregate(),plyr(),reshape2::cast, ordata.tabledepending on personal preference, readability, compactness, flexibility, speed … Here’s a simple solution withaggregate()that seems to do what you want:edit: re-reading/based on comment, this is not what you want: you need the summarized values replicated for each row, not just once per group.
Perhaps
although that gives a slightly weird data frame (the last “column” is actually a matrix).
It’s a little bit magical, but
is better — it unpacks the weird data frame returned by
aggregate()a bit more (the names are still a bit wonky).