Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column standard deviation R [duplicate]

Tags:

function

r

I was wondering if there was a built-in function in R that would compute the standard deviation for columns just like colMeans computes mean for every column. It would be simple enough to write my own mini function (a compound command that invokes things like apply with sd), but I was wondering if there was already something I could use whilst also keeping my code looking clean.

like image 381
Christian Bueno Avatar asked Aug 04 '13 21:08

Christian Bueno


People also ask

How do you get the standard deviation of a column in R?

To calculate the standard deviation in r, use the sd() function. The standard deviation of an observation variable in R is calculated by the square root of its variance. The sd in R is a built-in function that accepts the input object and computes the standard deviation of the values provided in the object.

How do I get sd from multiple columns in R?

Get standard deviation of multiple columns R using colSds() : Method 1. ColSds() Function along with sapply() is used to get the standard deviation of the multiple column. Dataframe is passed as an argument to ColSds() Function. standard deviation of numeric columns of the dataframe is calculated.

What is sd () in R?

sd() function is used to compute the standard deviation of given values in R. It is the square root of its variance. Syntax: sd(x) Parameters: x: numeric vector.

How do you find the mean of multiple columns in R?

To find the mean of multiple columns based on multiple grouping columns in R data frame, we can use summarise_at function with mean function.


2 Answers

The general idea is to sweep the function across. You have many options, one is apply():

R> set.seed(42) R> M <- matrix(rnorm(40),ncol=4) R> apply(M, 2, sd) [1] 0.835449 1.630584 1.156058 1.115269 R>  
like image 89
Dirk Eddelbuettel Avatar answered Oct 13 '22 12:10

Dirk Eddelbuettel


Use colSds function from matrixStats library.

library(matrixStats) set.seed(42) M <- matrix(rnorm(40),ncol=4) colSds(M)  [1] 0.8354488 1.6305844 1.1560580 1.1152688 
like image 36
MYaseen208 Avatar answered Oct 13 '22 12:10

MYaseen208