Standard deviation of a column in R can be calculated by using sd() function. sd() Function takes column name as argument and calculates the standard deviation of that column. standard deviation of single column in R, standard deviation of multiple columns using dplyr. Get row wise standard deviation. Let’s see how to calculate standard deviation in R with an example
- standard deviation of the single column – sd() function
- standard deviation of multiple columns in R
- standard deviation of Multiple columns in R using dplyr
- Find standard deviation of the column by column name
- Find standard deviation of the column by column position
- Get Row wise standard deviation in R
Let’s first create the dataframe.
### Create Data Frame df1 = data.frame(Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), Grade_score=c(4,6,2,9,5,7,8), Mathematics1_score=c(45,78,44,89,66,49,72), Science_score=c(56,52,45,88,33,90,47)) df1
So the resultant dataframe will be
Get Standard deviation of the column in R: Get Standard deviation of the column by column name
Method 1:Get Standard deviation of the column by column name
# Get Standard deviation of the column by column name sd(df1$Mathematics1_score)
Result:
Get Standard deviation of the column in R: Get Standard deviation of the column by column position
Method 2: Get Standard deviation of the column by column position
# Get Standard deviation of the column by column position sd(df1[,3])
Result:
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.
# Get standard deviation of the multiple columns colSds(as.matrix(df1[sapply(df1, is.numeric)]))
standard deviation of numeric columns of the dataframe will be
Get standard deviation of multiple columns in R using Dplyr : Method 2
summarise_if() Function along with is.numeric is used to get the standard deviation of the multiple column . With the help of summarise_if() Function, standard deviation of numeric columns of the dataframe is calculated.
# Get standard deviation of the multiple columns using dplyr library(dplyr) df1 %>% summarise_if(is.numeric, sd)
standard deviation of numeric columns of the dataframe will be
Get Row wise standard deviation in R:
Let’s calculate the row wise standard deviation of mathematics1_score and science_score as shown below
# Get Row wise standard deviation in R df1$SD_score = rowSds(as.matrix(df1[,c(3,4)])) df1
so the resultant dataframe with row wise standard deviation calculated will be
Other Related Topics: