add column to data frame based on conditions

Good day everyone, been trying to find a way to add a column based on conditions inside the same dataframe , for example using mtcars how can I multiply by 2 all the rows that meet condition mpg*cyl=126 and add the result in another column at the end? those that meet the condition will add the result and those that not will put a 0 as a result:

thanks a lot.

It's not clear to me what you mean by "multiply by 2 all the rows", but this works for the condition part.

library(tidyverse)
mtcars %>%
    mutate(new_col = if_else(mpg*cyl == 126.0, 2, 0)) %>% 
    head()
#>    mpg cyl disp  hp drat    wt  qsec vs am gear carb new_col
#> 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4       2
#> 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4       2
#> 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1       0
#> 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1       0
#> 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2       0
#> 6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1       0

Created on 2019-03-20 by the reprex package (v0.2.1)

1 Like

I am not sure I got your desired condition correct, but here is one possibility using mutate() from the dplyr package.

library(dplyr)

mtcars <- mtcars %>% mutate( NewCol = ifelse(mpg * cyl >= 126, 2 * mpg * cyl, 0))

Created on 2019-03-20 by the reprex package (v0.2.1)

1 Like

How to multiply by 2 all the rows that meet condition mpg*cyl=126 and add the result in another column at the end? Those that meet the condition will add the result and those that not will put a 0 as a result.

Hi! It sounds like you're trying to multiply the subset filter(mtcars, mpg * cyl == 126) by 2, zero-fill the subset filter(mtcars, mpg * cyl != 126), and preserve the existing columns in mtcars.

library(tidyverse)
set.seed(321)

group_nest(mtcars, .p = mpg * cyl == 126) %>% 
  transmute(data, 
            map_if(data, .p, 
                   ~ .x * 2, 
                   .else = ~ mutate_all(.x, ~ 0))) %>% 
  unnest() %>% 
  sample_n(5)
#> # A tibble: 5 x 22
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb  mpg1
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1  21       6  160    110  3.9   2.62  16.5     0     1     4     4    42
#> 2  21.4     4  121    109  4.11  2.78  18.6     1     1     4     2     0
#> 3  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4     0
#> 4  21       6  160    110  3.9   2.88  17.0     0     1     4     4    42
#> 5  17.3     8  276.   180  3.07  3.73  17.6     0     0     3     3     0
#> # … with 10 more variables: cyl1 <dbl>, disp1 <dbl>, hp1 <dbl>,
#> #   drat1 <dbl>, wt1 <dbl>, qsec1 <dbl>, vs1 <dbl>, am1 <dbl>,
#> #   gear1 <dbl>, carb1 <dbl>

Created on 2019-03-20 by the reprex package (v0.2.1)

1 Like

Great!, thanks a lot everyone it worked perfectly with the provided examples.

A post was split to a new topic: Reactive values not working

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.