psagg | R Documentation |
Take a vector or list and return the first element (pseudo-aggregation or projection). If the argument length is zero or there are different items throw in an error.
psagg(x, ..., strict = TRUE)
x |
should be a vector or list of items. |
... |
force later arguments to be passed by name |
strict |
logical, should we check value uniqueness. |
This function is useful in some split by column situations as a safe and legible way to convert vectors to scalars.
x[[1]] (or throw if not all items are equal or this is an empty vector).
d <- data.frame(
group = c("a", "a", "b"),
stringsAsFactors = FALSE)
dl <- lapply(
split(d, d$group),
function(di) {
data.frame(
# note: di$group is a possibly length>1 vector!
# pseudo aggregate it to the value that is
# constant for each group, confirming it is constant.
group_label = psagg(di$group),
group_count = nrow(di),
stringsAsFactors = FALSE
)
})
do.call(rbind, dl)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.