I am new to the purrr
package and am trying to iterate through each group in a dataframe to:
Cap the values of a variable (
Sepal.Length
) to a valuexmax
(e.g. 5), which is based on a quantile of the data for that groupSet the
x-axis
labels to be, for e.g. 0, 1, 2, 3, 4, >=5
I have one working method, but cannot get the following to work (note that this has been edited with thanks to the comments by @Jimbou). Columns xmax
, xbreaks
, xlabels
are created, but Sepal.Length
is a new column and I would instead like to update data$Sepal.Length
.
binwidth <- 1
graphs <- as_tibble(iris) %>%
nest(-Species) %>%
mutate(xmax = map(data, ~ plyr::round_any(quantile(.$Sepal.Length, 0.975), binwidth)),
xbreaks = map(xmax, ~ seq(0, ., binwidth)),
xlabels = map(xmax, ~c(seq(0, (. - binwidth), binwidth), paste0(">=", .))),
Sepal.Length= map2(data, xmax, ~ ifelse(.x$Sepal.Length >= .y, .y, .x$Sepal.Length)),
# this creates a new column, want it instead to update column in data
# a work-around would be to create a dataframe from the new column
# but I would like to work out how to update columns ...
graphs = map2(data, Species, ~ ggplot(., aes(Sepal.Length))) +
geom_histogram() +
scales_x_continuous(breaks=xbreaks, labels = xlabels) +
ggtitle(.y)
)
Thanks for your help.