0

I was trying to take the mean of some columns using across and there was an issue making new different columns on for each column of the mean I used. Is it working properly?

    library(tidyverse)
    cars %>% 
as_tibble() %>% 
add_case(speed = 11, dist = NA, .before = 1) %>% 
add_column(names = str_c("a",1:51)) %>% 
rename_with(.cols =  -names, ~str_c("one_",.x)) %>% 
group_by(names) %>% 
mutate(two = across(starts_with("one"), .fns = mean))

In the vignette it shows this example:

df %>% mutate_at(vars(c(x, starts_with("y"))), mean)
# ->
df %>% mutate(across(c(x, starts_with("y")), mean, na.rm = TRUE))

I would expect that in every case with NA it would produce NA instead of another column.

K Y
  • 198
  • 2
  • 16
  • Is this an answer? Because I haven't seen and difference. – K Y Dec 07 '20 at 10:28
  • Maybe I wasn't clear. I looking in the end to have a new column with name "two" in which I would have the mean of the other two columns – K Y Dec 07 '20 at 10:33

1 Answers1

1

Don't necessarily see the use of across here if you want to take rowwise mean of two columns.

library(dplyr)
cars %>% 
  as_tibble() %>% 
  add_case(speed = 11, dist = NA, .before = 1) %>% 
  add_column(names = str_c("a",1:51)) %>% 
  rename_with(.cols =  -names, ~str_c("one_",.x)) %>% 
  mutate(two = rowMeans(select(., starts_with('one')), na.rm = TRUE))

You can use rowwise with c_across but it is going to be inefficient then rowMeans.

cars %>% 
  as_tibble() %>% 
  add_case(speed = 11, dist = NA, .before = 1) %>% 
  add_column(names = str_c("a",1:51)) %>% 
  rename_with(.cols =  -names, ~str_c("one_",.x)) %>% 
  rowwise() %>%
  mutate(two = mean(c_across(starts_with('one')), na.rm = TRUE))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • I would like to do the rowmean on several columns but different groups. Better I guess rowMeans, that trying to complicate with across. – K Y Dec 07 '20 at 10:42
  • 1
    Yes, I guess this is an easier option. Did it solve your problem? – Ronak Shah Dec 07 '20 at 11:08
  • Yes, unfortunately, I haven't thought about rowMean in the first place which makes everything easier. SHould I delete the question because it is a duplicate probably? – K Y Dec 07 '20 at 11:15
  • 1
    You should not delete the question if it is duplicate, mark it as duplicate question instead. See https://meta.stackoverflow.com/questions/265736/should-i-delete-my-question-if-it-is-marked-as-a-duplicate – Ronak Shah Dec 07 '20 at 11:25