0

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.

0

2 Answers 2

2

Is this the function you are looking for?

add_x2 <- function(df, x) {
  df[paste0(x, "2")] <- df[x]^2
  df
}

df %>%
  add_x2(c("a", "b"))

# A tibble: 3 x 6
      a     b     c     d    a2    b2
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     4     7    10     1    16
2     2     5     8    11     4    25
3     3     6     9    12     9    36
Sign up to request clarification or add additional context in comments.

3 Comments

Yes but I want to to be applied on 100 columns at once. It seems the for loop is the best simple solution here aren't it. I want to build and expand my dataframe with new columns each iteration.
couldn't you just use names(df)[1:100] (or whatever columns you want) as the second argument to add_x2?
@steves Updated a bit so you can use it as suggested by IceCreamToucan
0

With tidyverse:

df %>% 
+   mutate_if(is.numeric,funs(.^2))
# A tibble: 3 x 4
      a     b     c     d
  <dbl> <dbl> <dbl> <dbl>
1     1    16    49   100
2     4    25    64   121
3     9    36    81   144

or

df %>% mutate_all(funs(.^2))
# A tibble: 3 x 4
      a     b     c     d
  <dbl> <dbl> <dbl> <dbl>
1     1    16    49   100
2     4    25    64   121
3     9    36    81   144

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.