icedcoffee
icedcoffee

Reputation: 1015

Find the mean of multiple columns in dplyr.... why am I getting a repeated value?

I have an object x:

x <- structure(list(ID = c("id1", "id2", "id3", "id4", 
"id5", "id6"), FC_C1 = c(1.0237284417215, -1.17547380888546, 
1.24032256483382, -1.26788878671502, -1.02667652234543, 1.08328144665509
), FC_C2 = c(-1.05037736352335, -1.51035519579208, 1.73843567887288, 
-1.06460883435243, -1.1937810921512, 1.32473926867105), FC_C3 = c(-1.04105748916685, 
-1.10813259308926, 1.33943213640751, -1.08323491305538, 1.0928771895575, 
1.20931550695441)), row.names = c(NA, 6L), class = "data.frame")

> x
   ID     FC_C1     FC_C2     FC_C3
1 id1  1.023728 -1.050377 -1.041057
2 id2 -1.175474 -1.510355 -1.108133
3 id3  1.240323  1.738436  1.339432
4 id4 -1.267889 -1.064609 -1.083235
5 id5 -1.026677 -1.193781  1.092877
6 id6  1.083281  1.324739  1.209316

I want a new column, called FC_average which is the average of the first three columns (for each row).

However, when I run this function:

meta_x <- x %>% 
    dplyr::mutate(
        FC_average = mean(c(
            FC_C1,
            FC_C2,
            FC_C3)
        ))

I get this:

> meta_x
   ID     FC_C1     FC_C2     FC_C3  FC_average
1 id1  1.023728 -1.050377 -1.041057 -0.08163635
2 id2 -1.175474 -1.510355 -1.108133 -0.08163635
3 id3  1.240323  1.738436  1.339432 -0.08163635
4 id4 -1.267889 -1.064609 -1.083235 -0.08163635
5 id5 -1.026677 -1.193781  1.092877 -0.08163635
6 id6  1.083281  1.324739  1.209316 -0.08163635

Why is the FC_average column not finding the mean per row?

Upvotes: 2

Views: 53

Answers (2)

SEAnalyst
SEAnalyst

Reputation: 1211

apply() can be used to iterate any function across either rows or columns. If you want the mean of each row, you could use this code:

apply(x[,2:4], 1,mean) -> x$FC_average

If for some reason you wanted column means, you would change the 1 to a 2 as in:

apply(x[,2:4], 2, mean)

In your code, you are asking to mutate() x with a new column that is a single value-- the mean of those three columns, or -0.08163635. Here is how you could use your existing code with rowwise()

x %>% 
  rowwise(.) %>% dplyr::mutate(
    FC_average = mean(c(
      FC_C1,
      FC_C2,
      FC_C3)
    ))

Upvotes: 1

akrun
akrun

Reputation: 887213

We can use vectorized rowMeans instead of mean

library(dplyr)
x %>% 
    mutate(FC_average = rowMeans(select(., starts_with('FC'))))

Upvotes: 2

Related Questions