[R] sd() for numeric row entries

Jim Moon moonja at ohsu.edu
Wed Dec 8 23:55:18 CET 2010

This is very helpful, Phil.  Being new to R and trying to get a handle on *apply, by, aggregate...  this is great.  Thank you.


-----Original Message-----
From: Phil Spector [mailto:spector at stat.berkeley.edu] 
Sent: Wednesday, December 08, 2010 2:36 PM
To: Jim Moon
Cc: r-help at r-project.org
Subject: Re: [R] sd() for numeric row entries

Jim -
    Suppose the data frame is named "df".  Then

df$std_dev = apply(df,1,sd,na.rm=TRUE)

will do what you want, although it will generate
some warning messages due to the first column.

df$sd_dev = apply(df[,sapply(df,is.numeric)],1,sd)

does the same, but won't produce warnings.

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu

On Wed, 8 Dec 2010, Jim Moon wrote:

> How might one calculate standard deviation, row-wise, for the numeric values in a data frame such as this one
>         V1     V2     V3     V4     V5
> 1 rs11089130 0.4565 0.4574 0.4569 0.4572
> 2   rs738829 0.6548 0.6519 0.6448 0.6549
> 3   rs915674 0.7503 0.7500 0.7517 0.7502
> and place the standard deviation in a 6th column called ???std_dev????
> Regards,
> Jim
> 	[[alternative HTML version deleted]]

More information about the R-help mailing list