Given a dataframe I want to run on multiple column names, calculate something and add the output as new column. The next calculation will be added as a new column to the updated dataframe.
For example:
Given a simple df:
df <- structure(list(a = c(1, 2, 3), b = c(4, 5, 6), c = c(7, 8, 9),
d = c(10, 11, 12)), .Names = c("a", "b", "c", "d"), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))
For each column a, b, c, d I want to calculate, say a square:
a2 = a^2
b2 = b^2 ...
For technical reasons I can't publish the whole dataframe but I am going to pass a column name each time and expect the function to mutate a new column (for example a2) next time when I will add b2, a2 will be already there:
If I would use for loop it would look like:
for (x in column_names) {
df <- df %>% mutate("x2" = x^2)
}
So each time my df updates with new calculated column.
Please advise how can I do this without for loop with functional programming.
I am trying to do this with map, lapply but I have the problem that my df doesn't get updated each iteration.