-2

i wish to calculate the mean of a column given the condition of another column

For example, Mean of Column 2 when Column 1 = Male That would mean: (69+55+60)/3

However, i am not too sure how to use the command.

This is my code: mean(Column2[,2](which(Column1[,1]=="M")))

Please kind advise thank you!

Also, i attached a simplified version of the data that i am doing. enter image description here enter image description here

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
Xiang De
  • 11
  • 1
  • Does this answer your question? [Mean per group in a data.frame](https://stackoverflow.com/questions/21982987/mean-per-group-in-a-data-frame) – mnist Apr 30 '20 at 21:33

4 Answers4

0

There are many ways you can do this. You can subset, you can aggregate, you can ddply, and many more!

df <- data.frame(Sex = c("F","F","M","M","M"),
                 Weight = c(50,44,69,55,60),
                 Height = c(160,165,179,185,177))
female <- subset(df, Sex == "F")
mean(female$Weight)
male <- subset(df, Sex == "M")
mean(male$Weight)
aggregate(df$Weight, list(df$Sex), mean)
library(plyr)
ddply(df, "Sex", summarise, mean = mean(Weight))
  • Hi! Thank you so much for commenting! Unfortunately this was not the answer i was looking for, but still good to know for future coding references! :) – Xiang De Apr 30 '20 at 23:58
0
# Data frame set up:
gender <- as.factor(c('F','F','M','M','M'))
weight <- c(50,44,69,55,60)
height <- c(160,165,179,185,177)
(df <- data.frame(gender,weight,height))

# Actual calculation:
mean(df[df$gender=='M',2])
# 61.333
Antoni Parellada
  • 4,253
  • 6
  • 49
  • 114
  • Hi! Thank you for your comment! But what is the "2" in [df$gender=='M',2]? – Xiang De Apr 30 '20 at 23:44
  • Hi! nevermind, i know what it is!, Took me some time to realize that it was column 2!. Thank you so much for your comment and help! – Xiang De Apr 30 '20 at 23:57
-1

Maybe aggregate is the thing you are looking for

dfout <- aggregate(.~Sex,df,mean)

such that

> dfout
  Sex   Weight   Height
1   F 47.00000 162.5000
2   M 61.33333 180.3333

Data

df <- structure(list(Sex = structure(c(1L, 1L, 2L, 2L, 2L), .Label = c("F", 
"M"), class = "factor"), Weight = c(50, 44, 69, 55, 60), Height = c(160, 
165, 179, 185, 177)), class = "data.frame", row.names = c(NA, 
-5L))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • Hi! Thank you so much for commenting! Unfortunately this was not the answer i was looking for, but still good to know for future coding references! :) – Xiang De Apr 30 '20 at 23:58
-1

You can use tapply, which applies a function--here, mean--to a table:

tapply(df$Weight, df$Sex, mean)

The result is the average weight per sex:

       F        M 
47.00000 61.33333
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
  • Hi! Thank you so much for commenting! Unfortunately this was not the answer i was looking for, but still good to know for future coding references! :) – Xiang De Apr 30 '20 at 23:58