I have a function which takes as inputs a data frame and additional arguments which determine some type of change to the data frame. As a simple example:
col_with_ones <- function(df, col_name) {
df[[col_name]] <- 1
df
}
Is there a way I can use Reduce
(or any other 'high-level' function) to apply multiple changes to a dataframe? e.g. continuing the example above, could I use Reduce
to do the following:
df <- data.frame(a = runif(10))
for (letter in letters[2:5]) {
df <- col_with_ones(df, letter)
}
Cheers
Quite simply:
Reduce(col_with_ones, letters[2:5], init = df)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With