Question: I am trying to compute average scores for responses to different events. My data is in long format with one row for each event, sample dataset `data` here:

```
Subject  Event   R1  R2 R3 R4   Average
1        A       1   2  2  N/A   2.5
1        B       1   1  1  1     1

```

So to get the average for event A, it would be (R1 + R2 + R3)/3 ignoring the N/A, whereas event B has 4 responses. I computed the average for Event A in `dplyr` as: 

```
data$average <- data%>%filter(Event == "A") %>% with(data, (R1 + R2 + R3)/4) 

```

I ran into problems when I tried to do the same for the next event...Thank you for the help!

Answer: 
The following doesn't include the NA value as part of the mean calculation (*na.rm=TRUE*). Also, I think grouping by Event is important. When run without group\_by, the calculations combine all events and the resulting value is 1.285714 (=9/7 obs).

```
data <- data.frame(
  Subject=c(1,1),
  Event=c('A', 'B'),
  R1=c(1,1),
  R2=c(2,1),
  R3=c(2,1),
  R4=c(NA,1)
)

df <- data %>%
  group_by(Event) %>%
  mutate(Average = mean(c(R1,R2,R3,R4), na.rm=TRUE))

```

Output:

```
Subject Event    R1    R2    R3    R4 Average
    <dbl> <fct> <dbl> <dbl> <dbl> <dbl>   <dbl>
1       1 A         1     2     2    NA    1.67
2       1 B         1     1     1     1    1   

```
Just to complete all previous answers, if you have multiple values named `R1`, `R2`, .... `R100`, instead of writing all of them into the `mean` function, you could be interested by reshaping your dataframe into a longer format using `pivot_longer` function and then group by Event and calculate the mean. Finally, using `pivot_wider`, you could get your dataframe into the initial wider format.

```r
library(dplyr)
library(tidyr)
df %>% mutate_at(vars(contains("R")), as.numeric) %>%
    pivot_longer(cols = starts_with("R"), names_to = "R", values_to = "Values") %>%
    group_by(Event) %>%
    mutate(average = mean(Values, na.rm = TRUE)) %>%
    pivot_wider(names_from = R, values_from = Values)

# A tibble: 2 x 8
# Groups:   Event [2]
  Subject Event Average average    R1    R2    R3    R4
    <int> <chr>   <dbl>   <dbl> <dbl> <dbl> <dbl> <dbl>
1       1 A         2.5    1.67     1     2     2    NA
2       1 B         1      1        1     1     1     1

```

As mentioned by @TTS, there is something wrong in your calculation of the average of the event A.

**Reproducible example**

```r
structure(list(Subject = c(1L, 1L), Event = c("A", "B"), R1 = c(1L, 
1L), R2 = 2:1, R3 = 2:1, R4 = c("N/A", "1"), Average = c(2.5, 
1)), row.names = c(NA, -2L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x5555743c1310>)

```