[R] Mean of a row of a data frame

Sorkin, John jsorkin at som.umaryland.edu
Wed Mar 21 03:18:46 CET 2018


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]]



More information about the R-help mailing list