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:> roopx 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 > 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.
> mean(list(1,4,0))[1] NA Warning message: In mean.default(list(1, 4, 0)) : argument is not numeric or logical: returning NA> mean(unlist(roop[1,]))[1] 1.666667> apply(roop, 1, mean)[1] 1.666667 5.333333 3.000000 data.frame is a list with some matrix characteristics. The list characteristics are winning in this example. Look at the definition of apply. It has an as.matrix in it that removes the list behavior. Rich On Tue, 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.
rowMeans is designed for speed. It also has as.matrix inside it. On Tue, Mar 20, 2018 at 10:40 PM, Boris Steipe <boris.steipe at utoronto.ca> wrote:> 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. > > ______________________________________________ > 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.