[R] Mean of a row of a data frame

Boris Steipe boris.steipe at utoronto.ca
Wed Mar 21 03:40:14 CET 2018


R > rowMeans(roop)
[1] 1.666667 5.333333 3.000000
R > mean(as.numeric(roop[1,]))
[1] 1.666667


:-)




> On Mar 20, 2018, at 10:18 PM, Sorkin, John <jsorkin at som.umaryland.edu> wrote:
> 
> I am trying to get the mean of a row of a data frame. My code follows:
> 
> 
> roop <- data.frame(x=c(1,2,3),y=c(4,5,2),z=c(0,9,4))
> roop
> mean(roop[1,])
> mean(roop[1,c("x","y","z")])
> 
> 
> I get the following output:
> 
>> roop
>  x y z
> 1 1 4 0
> 2 2 5 9
> 3 3 2 4
>> mean(roop[1,])
> [1] NA
> Warning message:
> In mean.default(roop[1, ]) :
>  argument is not numeric or logical: returning NA
>> mean(roop[1,c("x","y","z")])
> [1] NA
> Warning message:
> In mean.default(roop[1, c("x", "y", "z")]) :
>  argument is not numeric or logical: returning NA
> 
> 
> It is clear the the mean function does not like the data I am passing to it. I do not know why. How might I correct my code?
> 
> 
> As always,
> 
> Thank you,
> 
> John
> 
> 
> Thank you,
> 
> John
> 
> 
> 
> 
> 
> John David Sorkin M.D., Ph.D.
> Professor of Medicine
> Chief, Biostatistics and Informatics
> University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
> Baltimore VA Medical Center
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> (Phone) 410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list