简体   繁体   中英

Calculate mean of data frame by row

Is there a way to calculate the mean of a row from a data frame?

So for example:

df <- data.frame(X = c(1,1,1,1,2,2), Y = c(1,4,4,4,4,3), Z = c(2,5,6,8,3,1))

df
  X Y Z
1 1 1 2
2 1 4 5
3 1 4 6
4 1 4 8
5 2 4 3
6 2 3 1

So how can I calculate the mean per row?

mean(df[,1:3])

I really don't understand why this won't work, I mean the same code works for example for min() or max() but not for mean() .

min(df[,1:3])

[1] 1

When you say df[,1:3] you are choosing all rows of df and columns 1:3 . When you apply min or max to that, it simply looks for the min/max among all numbers. It is not doing it by row .

So when yo try to apply the same logic to mean , it again finds the mean value among all numbers in all three columns. Again, not by row .

You need to apply a function to a dimension of df . For this, use apply(df, 1, mean) as PKumar suggested. If you need the mean for each column, you say apply(df, 2, mean) . To learn more about apply type ?apply on the R console.

rowMeans and colMeans are shortcuts for apply .

To do this you need to use apply function you can compute the mean of all the rows by using the following syntax

apply(df,1, mean)
[1] 1.333333 3.333333 3.666667 4.333333 3.000000 2.000000

#when the second argument is 1, you are computing mean for each row, if it is set to 2 then you are computing for each column

To compute the mean of a specific row you need to subset the row use the following code

> apply(df[4,],1, mean) #here you compute the mean of the 4th row
#output
       4 
4.333333 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM