How to use shapiro wilk test to check normality of an R data frame column?


To apply shapiro wilk test for normality on vectors, we just simply name the vector inside shapiro.test function but if we want to do the same for an R data frame column then the column will have to specify the column in a proper way. For example, if the data frame name is df and the column name is x then the function will work as shapiro.test(df$x).

Example

 Live Demo

x1<-rnorm(1000,1.5)
df1<-data.frame(x1)
shapiro.test(df1$x1)

Output

   Shapiro-Wilk normality test
data: df1$x1
W = 0.99886, p-value = 0.792

Example

 Live Demo

x2<-runif(1000,2,10)
df2<-data.frame(x2)
shapiro.test(df2$x2)

Output

   Shapiro-Wilk normality test
data: df2$x2
W = 0.9581, p-value = 2.562e-16

Example

 Live Demo

x3<-rpois(4000,2)
df3<-data.frame(x3)
shapiro.test(df3$x3)

Output

   Shapiro-Wilk normality test
data: df3$x3
W = 0.91894, p-value < 2.2e-16

Example

 Live Demo

x4<-rpois(4000,5)
df4<-data.frame(x4)
shapiro.test(df4$x4)

Output

   Shapiro-Wilk normality test
data: df4$x4
W = 0.97092, p-value < 2.2e-16

Example

 Live Demo

x5<-sample(1:5,5000,replace=TRUE)
df5<-data.frame(x5)
shapiro.test(df5$x5)

Output

   Shapiro-Wilk normality test
data: df5$x5
W = 0.88902, p-value < 2.2e-16

Example

 Live Demo

x6<-sample(1:10,5000,replace=TRUE)
df6<-data.frame(x6)
shapiro.test(df6$x6)

Output

   Shapiro-Wilk normality test
data: df6$x6
W = 0.93373, p-value < 2.2e-16

Example

 Live Demo

x7<-sample(1:100,5000,replace=TRUE)
df7<-data.frame(x7)
shapiro.test(df7$x7)

Output

   Shapiro-Wilk normality test
data: df7$x7
W = 0.9556, p-value < 2.2e-16

Example

 Live Demo

x8<-sample(2500:3500,5000,replace=TRUE)
df8<-data.frame(x8)
shapiro.test(df8$x8)

Output

   Shapiro-Wilk normality test
data: df8$x8
W = 0.95117, p-value < 2.2e-16

Example

 Live Demo

x9<-rbinom(5000,10,0.5)
df9<-data.frame(x9)
hapiro.test(df9$x9)

Output

   Shapiro-Wilk normality test
data: df9$x9
W = 0.96629, p-value < 2.2e-16

Example

 Live Demo

x10<-rbinom(5000,1000,0.5)
df10<-data.frame(x10)
shapiro.test(df10$x10)

Output

   Shapiro-Wilk normality test
data: df10$x10
W = 0.9993, p-value = 0.04748

Updated on: 08-Sep-2020

472 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements