How to deal with missings in rowMeans? How to keep the output of RowMeans with mutate?

Dear Community,

I want to calculate the mean across a specific amount of variables and tried using mutate with rowMeans.
So far, there are two difficulties about it:

a) I get an error that 'x' must be numeric - is that because of NA for missings? How to deal with it?

b) In other versions with mutate the new var is shown in the tibble, but not added/saved to the existing dataset? How can I really add and keep it?

I used this code:

ssco_str <- c("s1abc", "s1cde", "s1efg")
fhprofs %>% mutate(ssco_meanall = rowMeans(fhprofs, na.rm=TRUE),
              ssco_strmean = rowMeans(select(fhprofs, ssco_str, na.rm=TRUE)))

The tibble I get from another code looked like this (mean in output of the tibble but not saved in dataframe):
fhprofs %>%
select (s1abc, s1cde, s1efg ) %>%
mutate(
ssco_strmean=(s1abc+ s1cde,+s1efg)/3
)

A tibble: 60 x 4

                s1strgarbm                     s1strgarbo                  s1strgbkol                                ssco_strmean
                 <dbl+lbl>                      <dbl+lbl>                   <dbl+lbl>                                       <dbl>

1 5 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 5
2 5 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 5
3 4 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 4.67
4 5 [trifft voll und ganz zu] 1 [trifft voll und ganz zu] 5 [trifft voll und ganz zu] 3.67

... with 50 more rows

By the way: How can I add a tibble here that actually looks like a tibble?

Hi,

I think it will indeed be easier if you provide a sample dataset we can work with. Take a look at the post below on how to build a reprex and share code and data efficiently. A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:

Good luck

Okay, so this is the code for an example dataframe and the mean calculation, that did not work. Only problem now, I also get additional error messages about the type of data. What could be wrong about the data.frame now?

subset_ssco <- data.frame(
                 s1abc = c(1, 2, 3, 4, 5, 6, 7),
                 s1cde = c(3, 2, NA, 1, 1, NA, 4),
                 s1efg = c(10, 5, 2, NA, 3, 5, 3)
)
subset_ssco %>% mutate(
  ssco_meanall = rowMeans(
    select(
      s1abc, s1cde, s1efg, 
      na.rm=TRUE)))

Error in mutate():
! Problem while computing ssco_meanall = rowMeans(select(s1abc, s1cde, s1efg, na.rm = TRUE)).
Caused by error in UseMethod():
! no applicable method for 'select' applied to an object of class "c('double', 'numeric')"
Backtrace:

  1. subset_ssco %>% ...
  2. dplyr::select(s1abc, s1cde, s1efg, na.rm = TRUE)
    Error in mutate(., ssco_meanall = rowMeans(select(s1abc, s1cde, s1efg, :

Caused by error in UseMethod():
! no applicable method for 'select' applied to an object of class "c('double', 'numeric')"

1 Like

You can do this sort of thing :

subset_ssco %>% mutate(
  ssco_meanall = rowMeans(across(c(s1abc,s1cde,s1efg)),na.rm = TRUE))

subset_ssco %>% mutate(
  ssco_meanall = rowMeans(across(where(is.numeric)),na.rm = TRUE))

i.e. use across rather than select.

1 Like

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.