I have a list of data frames, where every data frame is similar (has the same columns with the same names) but contains information on a different, related "thing" (say, species of flower). I need an elegant way to re-categorize one of the columns in all of these data frames from continuous to categorical using the function cut()
. The problem is each "thing" (flower) has different cut-points and will use different labels.
I got as far as putting the cut-points and labels in a separate list. If we're following my fake example, it basically looks like this:
iris <- iris
peony <- iris #pretending that this is actually different data!
flowers <- list(iris = iris, peony = peony)
params <- list(iris_param = list(cutpoints = c(1, 4.5),
labels = c("low", "medium", "high")),
peony_param = list(cutpoints = c(1.5, 2.5, 5),
labels = c("too_low", "kinda_low", "okay", "just_right")))
#And we want to cut 'Sepal.Width' on both peony and iris
I am now really stuck. I have tried using some combinations of lapply()
and do.call()
but I'm kind of just guessing (and guessing wrong).
More generalized, I want to know: how can I use a changing set of arguments to apply a function over different data frames in a list?