Find Mean Value in a Column

Hi. I'm trying to compare the average baby's birth weights (bwt) among smoking mothers and non-smoking mother. There is a column 'smoke', where 1 represents smoking mothers and 0 represents non-smoking mom. Here is what I have:

 babies<-read.table("~/Desktop/babies.txt",header=T)
 mean(babies$bwt[babies$smoke==1])
 mean(babies$bwt[babies$smoke==0])

And the answers I get are both NA which confuses me, can anyone please help? Thank you so much.

How does this work out?

mean(babies[babies$smoke==1, 'bwt'], na.rm = TRUE)
mean(babies[babies$smoke==0, 'bwt'], na.rm = TRUE)

Changes:

  • Order of terms slightly changed;
  • na.rm = TRUE is a setting to ignore missing cases, just in case you have any.
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.