How to use pnorm function on data frame columns in R?


The pnorm function is used to find the probability for a normally distributed random variable. Probabilities such as less than mean, greater than mean, or probability between left- and right-hand side of the mean. If we want to use pnorm function on data frame columns then apply function can help us.

Consider the below data frame −

Example

 Live Demo

x1<-rnorm(20,5,0.35)
x2<-rnorm(20,5,0.67)
x3<-rnorm(20,5,0.04)
df1<-data.frame(x1,x2,x3)
df1

Output

       x1    x2         x3
1  4.556392  5.973934   5.018973
2  5.217397  4.932053   4.975870
3  5.426464  4.932799   4.962231
4  4.930645  5.297919   5.017925
5  4.773804  4.768619   4.943131
6  4.963782  4.569909   4.950701
7  4.925481  5.329717   4.985630
8  4.940240  5.871122   5.007031
9  4.904643  5.270739   5.022102
10 4.652542  5.784937   5.005462
11 5.089297  4.479673   4.961000
12 5.619575  4.181733   4.983067
13 4.696906  4.451156   4.931908
14 5.177524  4.422826   5.052467
15 5.186783  5.184310   5.015104
16 4.497172  5.241887   4.996715
17 4.689212  5.252937   5.035001
18 5.385772  4.095684   5.035014
19 5.455497  5.142272   5.021073
20 5.417301  5.025720   5.005374

Applying pnorm on columns in df1 −

Example

apply(df1,2,function(x) pnorm(x,mean=mean(x),sd=sd(x)))

Output

       x1          x2           x3
[1,]  0.07616627  0.96450889   0.75138999
[2,]  0.72115750  0.44156102   0.27056837
[3,]  0.88960525  0.44211276   0.15403922
[4,]  0.38629544  0.70493965   0.74135388
[5,]  0.22132609  0.32516348   0.05581552
[6,]  0.42550072  0.20448316   0.08623025
[7,]  0.38027932  0.72516490   0.37486428
[8,]  0.39754810  0.94661794   0.62607863
[9,]  0.35630529  0.68712704   0.78009609
[10,] 0.12759048  0.92666438   0.60816173
[11,] 0.57741133  0.15991056   0.14545675
[12,] 0.96515143  0.06018775   0.34616630
[13,] 0.15806523  0.14725726   0.02700442
[14,] 0.67888286  0.13536904   0.95364621
[15,] 0.68893707  0.62769115   0.71330952
[16,] 0.05346986  0.66772918   0.50508628
[17,] 0.15246286  0.67521495   0.87668128
[18,] 0.86438253  0.04322155   0.87676402
[19,] 0.90541682  0.59753060   0.77087289
[20,] 0.88424194  0.51137989   0.60714737

Example

 Live Demo

y1<-rpois(20,5)
y2<-rpois(20,2)
y3<-rpois(20,2)
y4<-rpois(20,5)
y5<-rpois(20,10)
df2<-data.frame(y1,y2,y3,y4,y5)
df2

Output

   y1 y2 y3 y4 y5
1  7  4  3  3  10
2  7  2  2  5  6
3  2  1  4  4  11
4  5  1  2  6  13
5  6  2  3  9  10
6  7  4  4  4  7
7  5  3  2  7  15
8  2  1  1  3  15
9  3  1  2  4  9
10 4  3  1  4  15
11 1  4  4  4  13
12 5  6  4  8  9
13 3  0  5  2  14
14 7  2  1  8  7
15 6  3  4  5  10
16 3  2  2  6  19
17 4  1  5  5  11
18 7  2  1  5  11
19 6  1  2  9  9
20 3  3  4  3  9

Applying pnorm on columns in df2 −

Example

apply(df2,2,function(x) pnorm(x,mean=mean(x),sd=sd(x)))

Output

           y1        y2         y3           y4         y5
[1,]  0.88543697  0.87874297  0.55840970  0.14362005  0.36298572
[2,]  0.88543697  0.41829947  0.27834877  0.46146443  0.05825608
[3,]  0.08752759  0.18573275  0.81101173  0.28079874  0.48176830
[4,]  0.57107536  0.18573275  0.27834877  0.65061458  0.71356535
[5,]  0.75517414  0.41829947  0.55840970  0.96698029  0.36298572
[6,]  0.88543697  0.87874297  0.81101173  0.28079874  0.10296979
[7,]  0.57107536  0.68482707  0.27834877  0.80804251  0.87967779
[8,]  0.08752759   0.18573275 0.09300983  0.14362005  0.87967779
[9,]  0.19922632  0.18573275  0.27834877  0.28079874  0.25614928
[10,] 0.36970390  0.68482707  0.09300983  0.28079874  0.87967779
[11,] 0.03088880  0.87874297  0.81101173  0.28079874  0.71356535
[12,] 0.57107536  0.99451570  0.81101173  0.91220051  0.25614928
[13,] 0.19922632  0.05691416  0.94698775  0.06082067  0.80746817
[14,] 0.88543697  0.41829947  0.09300983  0.91220051  0.10296979
[15,] 0.75517414  0.68482707  0.81101173  0.46146443  0.36298572
[16,] 0.19922632  0.41829947  0.27834877  0.65061458  0.99163233
[17,] 0.36970390  0.18573275  0.94698775  0.46146443  0.48176830
[18,] 0.88543697  0.41829947  0.09300983  0.46146443  0.48176830
[19,] 0.75517414  0.18573275  0.27834877  0.96698029  0.25614928
[20,] 0.19922632  0.68482707  0.81101173  0.14362005  0.25614928

Updated on: 10-Feb-2021

538 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements