3

I want to change the values of a column, which to be called by it's index, using pipe -

require(dplyr) 
mtcars[, 1] = 4 * mtcars[,2]

I was wondering if above calculation can be done using pipe

M--
  • 25,431
  • 8
  • 61
  • 93
Bogaso
  • 2,838
  • 3
  • 24
  • 54
  • Not sure it is possible. Have a read at [this](https://stackoverflow.com/questions/32618744/dplyr-how-to-reference-columns-by-column-index-rather-than-column-name-using-mu) – Sotos Feb 25 '20 at 15:59
  • You can do something like `mtcars[, 1] <- mtcars %>% pull(1) %>% multiply_by(2)`. – tmfmnk Feb 25 '20 at 16:07

2 Answers2

3

You can use magrittr and %<>%:

mtcars -> df1

library(dplyr) 
library(magrittr)

df1 %<>% 
  mutate_at(vars(1), list(~ df1[[2]] * 4))

#> # A tibble: 32 x 11
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1    24     6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2    24     6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3    16     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4    24     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5    32     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6    24     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7    32     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8    16     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9    16     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10    24     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ... with 22 more rows
M--
  • 25,431
  • 8
  • 61
  • 93
  • This is quite awesome. However when I change df1[[2]] to .[[2]] I get wired result. Any idea how to fix that? – Bogaso Feb 25 '20 at 16:42
  • @Bogaso within `mutate_at` when you use **`.`** it refers to the `vars`, not the original dataset. When you use `.[[2]]` you are actually referring to the second row of `vars(1)` (i.e. `df1[1,2]`). – M-- Feb 25 '20 at 16:53
  • Thanks. So if I need to refer the original dataset under this framework, calling by name is only way? I want to avoid calling by name to keep the process tidy (this is the fundamental idea working with pipe) – Bogaso Feb 25 '20 at 17:14
  • 1
    @M-- If you have a `group_by`, then do you use `df1[[2]]`. I tried with `.data[[2]]`, but it is not working because the `.data` seems to be the column in `mutate_all` – akrun Feb 25 '20 at 18:09
  • @Bogaso keeping tidy and readability is important, but performance is also something you should think of. Glad that the other answer provides what you need. It's actually a smart one. – M-- Feb 25 '20 at 21:31
  • @akrun if I wanted to stick to this along with `group_by`, I would've probably explored `purrr` solutions. makes sense? p.s. (this whole question is a bit unclear, not in terms of what it wants, but why it wants it that way) – M-- Feb 25 '20 at 21:35
  • 1
    @M-- But, it can be done with `.data` as well, only issue is that `.data` requires the column names with `.data[[` (.data[[2]] is returning error for me. Not sure whether it is the feature or a bug). In the devel version`mtcars %>% group_by(vs) %>% mutate(across(cols = c(1), ~ mean(.data[[names(mtcars)[2]]])))` – akrun Feb 25 '20 at 21:38
  • 1
    @akrun I thought you want to avoid using `mtcars` (i.e. LHS name) within your pipes. You're right, `.data` works the way you have it :) – M-- Feb 25 '20 at 21:40
2

Another option could be:

mtcars %<>%
 mutate_at(vars(1), ~ !!select(., 2) %>% pull() * 4)

   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
1   24   6 160.0 110 3.90 2.620 16.46  0  1    4    4
2   24   6 160.0 110 3.90 2.875 17.02  0  1    4    4
3   16   4 108.0  93 3.85 2.320 18.61  1  1    4    1
4   24   6 258.0 110 3.08 3.215 19.44  1  0    3    1
5   32   8 360.0 175 3.15 3.440 17.02  0  0    3    2
6   24   6 225.0 105 2.76 3.460 20.22  1  0    3    1
7   32   8 360.0 245 3.21 3.570 15.84  0  0    3    4
8   16   4 146.7  62 3.69 3.190 20.00  1  0    4    2
9   16   4 140.8  95 3.92 3.150 22.90  1  0    4    2
10  24   6 167.6 123 3.92 3.440 18.30  1  0    4    4
tmfmnk
  • 38,881
  • 4
  • 47
  • 67
  • Perfect! One quick Q - Do we really need '%<>%' operator? I found it is equally working with "%>%" operator. – Bogaso Feb 25 '20 at 17:45
  • 1
    If you want to update the LHS with the new results, then yes. Take a look at `help("%<>%")`. – tmfmnk Feb 25 '20 at 18:02