Get Standard Deviation of a Column in R dataframe
In this article, we are going to find the standard deviation of a column in a dataframe in R Programming Language.
To select the desired column of a dataframe $ is used.
Syntax:
dataframe$column_name
Formula for variance:
where n is the total number of observations and x bar is the mean
Formula for standard deviation:
In the R programming language, for finding standard deviation on set of data, the method used is sd()
Syntax:
sd(data_values)
Where data-values are a vector input or data frame input.
Given below are some examples to help you understand this better
Example 1:
R
# data1 with vector of elements data1= c (1,2,3,4,5) # data2 with vector of elements data2= c ( "sravan" , 'bobby' , 'rohith' , 'gnanu' , 'ojaswi' ) # give input to the data which is a dataframe data= data.frame (a1=data1,a2=data2) # finding standard deviation of dataframe # column 1 print ( sd (data$a1)) |
Output:
[1] 1.581139
Example 2:
R
# data1 with vector of elements data1= c (1,2,3,4,5) # data2 with vector of elements data2= c (10,20,30,40,50) # give input to the data which is a # dataframe data= data.frame (a1=data1,a2=data2) # finding standard deviation of dataframe # column 1 print ( sd (data$a1)) # finding standard deviation of dataframe # column 2 print ( sd (data$a2)) |
Output:
[1] 1.581139
[1] 15.81139
Example 3:
R
# data1 with vector of elements (float # values) data1= c (1.0,2,3,4,5,8) # data2 with vector of elements(float # values) data2= c (10,20.5,30.3,40,50,67.89) # give input to the data which is a # dataframe data= data.frame (a1=data1,a2=data2) # finding standard deviation of dataframe # column 1 print ( sd (data$a1)) # finding standard deviation of dataframe # column 2 print ( sd (data$a2)) |
Output:
[1] 2.483277
[1] 20.86387
Please Login to comment...