Mean of a row of a data frame

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Mean of a row of a data frame

Sorkin, John
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Mean of a row of a data frame

Boris Steipe
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Mean of a row of a data frame

Richard M. Heiberger
In reply to this post by Sorkin, John
> 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
<[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Mean of a row of a data frame

Richard M. Heiberger
In reply to this post by Boris Steipe
rowMeans is designed for speed.  It also has as.matrix inside it.

On Tue, Mar 20, 2018 at 10:40 PM, Boris Steipe <[hidden email]> 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 <[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.