Updated value from mutate(across)) and rowSums

I stumbled upon a problem with across and rowSums in a single mutate today.

Let's say I have the following two dataframes:

iris1 <- iris %>% 
  mutate(across(Sepal.Length:Petal.Width, ~ 5 +.x),  
         sums = rowSums(select(.,Sepal.Length:Petal.Width)))

This does not what I want, because the "." in rowSums(select) selects the "old" values.

iris2 <- iris %>%
  mutate(across(Sepal.Length:Petal.Width, ~ 5 +.x)) %>%
  mutate(sums = rowSums(select(.,Sepal.Length:Petal.Width)))

This does what I want, but I need to use two mutates. Is there a way on how I can do it in a single mutate?

iris3 <-iris %>% 
  mutate(across(Sepal.Length:Petal.Width, ~ 5 +.x),
         sums = rowSums(across(Sepal.Length:Petal.Width, ~.x)))

I found another solution:

iris1 <- iris %>% 
  mutate(across(Sepal.Length:Petal.Width, ~ 5 +.x),  
         sums = rowSums(select(cur_data(),Sepal.Length:Petal.Width)))

This topic was automatically closed 7 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.