Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to modify a column named in another column using adjacent column in tidyverse?

I’ve the following dataframe,

df <- tibble(x = c(2, 3, 4)) %>% 
mutate(`1` = 99, `2` = 88, `3` = 77, `4` = 66, `5` = 55)

column x holds the column names which needs to be manipulated, the value in that column has to be replaced with the sum of values in columns x-1, x and x+1. For example, for the first row where x is 2, value in column 2 has to be replaced with (99+88+77) = 264.

I tried using double curly brackets({{}}) and :=, like below,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df %>% 
  mutate("{{x}}" := {{x-1}} + {{x}} + {{x+1}})

but I’m getting the following error,
Error in local_error_context(dots = dots, .index = i, mask = mask) : promise already under evaluation: recursive default argument reference or earlier problems?

Then I tried accessing the column using cur_column() inside across() like below,

df %>% 
  mutate(across(-x, ~if_else(x == cur_column(), {{cur_column()}}, .x)))

and I’m getting the same error as above, I think I may be using the curly operator incorrectly, can someone help, please?

>Solution :

The {{}} syntax is for when you are passing unevaluated expressions to a dplyr command, it does not work for capturing column values.

Most of the time it’s not easy to do different options on different columns for each row. One alternative is to reshape your data so you can use lead/lag functions. Then you can pivot back.

library(dplyr)
library(tidyr)
df %>% 
  mutate(row = row_number()) %>% 
  pivot_longer(!c(x, row)) %>% 
  mutate(name = as.integer(name)) %>% 
  group_by(row) %>% 
  mutate(value=if_else(x==name, value + lead(value) + lag(value), value)) %>% 
  pivot_wider(c(row, x)) %>% 
  select(-row)

#     row     x   `1`   `2`   `3`   `4`   `5`
#   <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1     1     2    99   264    77    66    55
# 2     2     3    99    88   231    66    55
# 3     3     4    99    88    77   198    55

Another alternative is to create a helper function that can access the cur_column() and cur_data() with rowwise() in order to create a different transformation for each row.


colclump <- function(target) {
  prevc <- as.character(as.integer(target)-1)
  nextc <- as.character(as.integer(target)+1)
  function(x) {
    if (cur_column()==target) {
      x + cur_data()[[prevc]] + cur_data()[[nextc]]
    } else {
      x
    }
  }
  
}
df %>% 
  rowwise() %>% 
  mutate(across(-x, ~colclump(x)(.x)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading