본문 바로가기
R

(R) lapply, sapply, mapply/ two-sample t-test using mapply function

by jangpiano 2020. 11. 17.
반응형

<lapply vs sapply> 


lapply function always returns the result of function as  a list from a list. 

sapply function always returns the result of functions as a vector from a list.  

> list_1=list(x=rnorm(5,0,1),y=runif(3,5,10))

> list_1

$x

[1] -0.1718824 -0.1953697 -0.3611449  1.1755966 -1.0458594


$y

[1] 7.881664 9.084847 9.950752



> list_2 = list(x = rnorm(10,0,1), y = c(rnorm(5,3,8), NA))


> list_2

$x

 [1]  1.76547974 -0.74886117 -1.14383381  0.73317284 -1.18059019  0.31822345  0.03321232

 [8] -1.14327293  0.28340189  0.64266838


$y

[1]   3.182408 -11.021280  10.861944   4.079862 -10.516394         NA


lapply function 

 sapply function

 lapply always returns the result of function as  a list. 

 sapply always returns the result of function as a vector, matrix. 

 > lapply(list_1, mean) 


$x

[1] -0.119732


$y

[1] 8.972421




> lapply(list_1, function(x) c(Mean = mean(x), SD = sd(x)))


$x

      Mean         SD 

-0.1197320  0.8065837 


$y

    Mean       SD 

8.972421 1.039115 


> lapply(list_2, function(x) c(Mean = mean(x, na.rm = T), SD = sd(x, na.rm = T)))


$x

       Mean          SD 

-0.04403995  0.98887082 


$y

      Mean         SD 

-0.6826919  9.6758414 

> sapply(list_1, mean)  


        x         y 

-0.119732  8.972421  




> sapply(list_1, function(x) c(Mean = mean(x), SD = sd(x)))


              x        y

Mean -0.1197320 8.972421

SD    0.8065837 1.039115


> sapply(list_2, function(x) c(Mean = mean(x, na.rm = T), SD = sd(x, na.rm = T)))

               x          y

Mean -0.04403995 -0.6826919

SD    0.98887082  9.6758414


  sapply function

 sapply function(simplify=F)

> sapply(list_1, mean)  


        x         y 

-0.119732  8.972421 

 sapply(list_1,mean,simplify=FALSE)

$x

[1] 0.7408509


$y

[1] 7.169339


<sapply vs mapply>

As we observe in the previous example, sapply returns a vector which is made from the function from list . 

mapply function is a multivariate version of the sapply function. That is, it can contains more than two arguments. 

> x1 = seq(30, 34, by = 2)

> x2 = seq(25, 35, by = 5)


the expand.grid function creates a data frame that contains a unique combination of x1 and x2.


> x1.x2 = expand.grid(x1, x2)

> x1.x2 

  Var1 Var2

1   30   25

2   32   25

3   34   25

4   30   30

5   32   30

6   34   30

7   30   35

8   32   35

9   34   35


[mapply function]


#<mapply to columns>


> x1 = seq(30, 34, by = 2)

> x1

[1] 30 32 34

> m.pred=function(x)(3*x+2)

> sapply(x1,m.pred).    #sapply function has a variable x1 this is the comparison point between sapply and mapply. 

[1]  92  98 104


>x2=seq(25,35,by=5)

> x1.x2=expand.grid(x1,x2)

> x1.x2

  Var1 Var2

1   30   25

2   32   25

3   34   25

4   30   30

5   32   30

6   34   30

7   30   35

8   32   35

9   34   35


> x1.x2[,1]            #first column

[1] 30 32 34 30 32 34 30 32 34

> x1.x2[,2].          #second column 

[1] 25 25 25 30 30 30 35 35 35


> m.pred=function(x,y)(3*x+y) 


> mapply(m.pred, x1.x2[,1], x1.x2[,2])    #element in first column as x, element in second column as y

[1] 115 121 127 120 126 132 125 131 137


> mapply(m.pred, x1.x2[1], x1.x2[2])

      Var1

 [1,]  115

 [2,]  121

 [3,]  127

 [4,]  120

 [5,]  126

 [6,]  132

 [7,]  125

 [8,]  131

 [9,]  137


> m.pred_2=function(x,y)((x+1)^2+y)


> mapply(m.pred_2,x1.x2[,1],x1.x2[,2])            #element in first column as x, element in second column as y 

[1]  986 1114 1250  991 1119 1255  996 1124

[9] 1260


#<mapply to rows>

 

> x1_x2=rbind(x1,x2)

> x1_x2

   [,1] [,2] [,3]

x1   30   32   34

x2   25   30   35


> m.pred=function(x,y)(x^2+10*y)

> mapply(m.pred , x1_x2[1,] , x1_x2[2,])         #element in first row as x and element of second row as y

[1] 1150 1324 1506


<t-test using mapply(student's, welch's)>


 

> student.t.test <- function(x1, x2) {

+   return(t.test(x1, x2, var.equal = TRUE)$p.value)

+   }

> welch.t.test<- function(x1, x2) {

+   return(t.test(x1, x2, var.equal = FALSE)$p.value)

+   }


> x = as.list(as.data.frame(matrix(rnorm(n=600, mean=10, sd=1),12,50)))

> y = as.list(as.data.frame(matrix(rnorm(n=600, mean=10, sd=10),12,50)))  

> x

$V1

 [1] 10.569413  9.730193 10.129384  8.045248  9.507468 11.152292  9.693162 10.369953

 [9] 11.217997 11.369700  9.747703 10.368824


$V2

 [1]  9.591829  9.639191 10.430691  8.713020  9.365734  9.796357  9.470041 10.463307

 [9] 10.788477 11.580841 10.189135  9.983516


$V3

 [1]  8.569551  8.630170 10.002310  9.774065  9.140203  9.710049 11.473954  9.009991

 [9] 11.907637 12.215771  8.916336  8.084277


$V4

 [1]  9.953234  8.256620  8.853046 11.252880 11.312386  9.982500  9.641947 11.921640

 [9]  9.914962 11.151792  9.055550 10.988085


$V5

 [1] 11.277386 10.756478 11.347668 11.300402  8.522250 10.156227  9.100721  8.253531

 [9]  9.731361  9.136377 10.641938 10.161165


$V6

 [1]  8.822972 10.060470 11.258291  9.792795  9.480411  9.839450 10.681277 10.433093

 [9]  9.153168 11.132416  9.885356 11.197503


$V7

 [1] 11.642025 10.058930 11.168744 10.637830  8.008920 10.348190 10.393254  8.751812

 [9] 10.421721 11.258496  8.643358  9.988735


$V8

 [1]  8.177030  9.934701 10.019830 11.683906 10.058874 12.479540 10.229505 11.896030

 [9] 10.591635 10.222968  9.793647  9.224552


$V9

 [1]  8.920025 11.631117 10.144509 10.156021  9.609770  9.553515 10.333560 10.602403

 [9]  8.792290 10.234035 10.136436 10.911875


$V10

 [1]  8.425614 10.029266 10.326481 10.304341 10.841362 10.742419 10.239214  9.794407

 [9]  9.547841 11.375985 10.204529  9.079666


$V11

 [1] 10.769752 10.343351  9.666470 11.934241 10.179899  8.424407 10.385962  8.215562

 [9] 10.711800  9.973383 10.488631 11.250497


$V12

 [1]  9.402043  9.545587 10.844684 11.157446  9.049619  9.690360  9.526292 10.989130

 [9]  9.652494  9.387936  9.706570 11.099918


$V13

 [1]  9.302132 10.202861 11.232037 10.295327  9.554729  9.921305  9.748471  8.983887

 [9]  9.438730 11.510385 10.673881 10.487738


$V14

 [1] 12.624131 11.070730  9.750799  9.722343 12.088167 12.996310 10.399936  8.711927

 [9] 10.204732 10.517572 10.600516  9.983142


$V15

 [1] 10.373477  9.490030 10.621390  9.769112  9.968036  9.159318 10.502239  9.609087

 [9]  9.115170  9.586645 10.442900  8.842283


$V16

 [1] 10.432843 10.901414  8.200111  9.305012 10.469562 11.607875 10.198993 10.863067

 [9] 10.353409 12.341936  8.662304 10.403412


$V17

 [1] 10.229715 10.721323  8.638300  9.386579  8.781183  9.698624 10.652363  9.983659

 [9]  9.980578 10.092645 10.939571  8.809258


$V18

 [1] 10.194143 11.651591  9.564575 10.406631  8.297892  9.476400 10.100086  9.296703

 [9] 10.338661 10.871579 10.300629 10.612365


$V19

 [1]  8.873063  9.931815 10.011985 10.040373  9.910884  8.004362  8.920872  9.966077

 [9] 10.515311 10.406475 10.128113 10.299180


$V20

 [1] 10.236077  8.735121  9.353080  9.856171  9.138792  7.821223 10.066676 10.317724

 [9] 11.221044 10.194843 10.835882 11.368091


$V21

 [1] 12.008723  9.114673  7.860459  9.309153 12.405971  9.810744 11.489657  8.588896

 [9] 10.522564 10.528186  8.996779  9.577241


$V22

 [1]  9.967495  8.836300 10.312496  9.412208 10.921231  9.930687  9.739548  7.510454

 [9]  9.851273 10.613795 10.046548  7.288920


$V23

 [1]  9.560169 10.533577  8.532837  9.439206  8.504566  8.613054  9.732412 11.552097

 [9]  9.315848 10.780875 10.590293 10.479729


$V24

 [1] 10.726895  9.868978 11.222230  9.938351  9.568987  9.425167 11.220548  8.297832

 [9] 10.969417  9.788625 11.117416  8.255932


$V25

 [1]  8.245885 11.036732  9.128759 10.349829 11.268845  9.701727  9.245582  8.510485

 [9] 10.564797 10.332632 10.307935 10.178132


$V26

 [1]  8.414678 10.296711  9.931577 10.990956  8.849011 11.733697 11.076057 10.510635

 [9] 10.980403 10.690568 10.126082  9.974705


$V27

 [1]  9.504747 11.394600 10.203338 10.212920 11.993722  8.416459  7.522640 10.195159

 [9]  8.162564  8.380668 11.487150 10.973510


$V28

 [1]  9.616056  9.051915  9.849974  8.200029 11.187892  9.792710  9.067202  9.443566

 [9] 10.875986 10.088456 10.002565  9.009843


$V29

 [1] 11.661979  9.914841 11.834506  9.788110  9.832921 10.568216 11.179088 11.446190

 [9] 10.867388  8.008688 10.396959 11.227422


$V30

 [1]  8.966381  9.331055  9.933053 10.745908 10.915905 11.875415  9.168041  9.934039

 [9] 11.021574  9.347111  8.603004 11.256605


$V31

 [1] 10.820390  9.861693  9.448015 11.314620  9.408272 11.959150  8.651807 11.955363

 [9]  8.992959  8.952279 11.771601  9.562229


$V32

 [1] 10.889923  9.804776 10.707593  9.996622  9.684147 10.276712 10.446140  9.385195

 [9]  8.584946 11.293675 10.706834 11.036381


$V33

 [1]  8.780660 11.306854  9.238334 10.746422  9.898093 11.656677  9.461758 11.092331

 [9] 10.938518  8.367700 10.352973  8.367915


$V34

 [1]  8.924215  9.194572 10.106072 10.210378  9.175591  9.413795 11.740399 10.638750

 [9] 10.305073 11.223488 10.895922  9.463377


$V35

 [1] 10.051645  9.086643 10.508699  8.180019 11.646602 10.020107  9.353698 10.750617

 [9] 10.031351 11.216116  9.282011 10.722776


$V36

 [1]  9.816137 10.599976  9.385796  8.521524  9.407166 10.988144 11.455262 10.148820

 [9]  9.580258  9.455175  9.734448  8.674892


$V37

 [1]  9.593316  9.997669  9.558957 11.076040  9.605183  9.179733 10.895369 10.643794

 [9]  9.516408 10.692357  9.589148 10.123699


$V38

 [1] 10.494317  9.422831 10.488669  8.966916 12.311095 10.468764  9.969253 10.154723

 [9] 11.111244  9.104623 10.872649  9.529517


$V39

 [1] 11.422475  9.948190 10.156749 11.122148 10.667450  9.228860 11.214646  8.159029

 [9] 10.429625  8.060370 10.869972 10.808402


$V40

 [1] 11.307840 10.074630  8.961456  9.462628 10.024067 10.316038 10.391914  9.099028

 [9] 10.861027 10.216991 10.563809 10.299955


$V41

 [1] 10.995194  9.757476 10.953527  9.579378  8.646744  8.652240 10.569014 10.140191

 [9]  8.769270 10.293096 10.876544 11.204256


$V42

 [1]  9.638297  9.859206  9.043634  9.669574 10.191488 11.236970  9.020469 10.857576

 [9]  9.227174 10.051267 10.383126 10.176865


$V43

 [1] 10.625591 10.216699 12.170783  7.978018 10.306694 10.822960  8.558889 10.093585

 [9]  9.478182  9.679941  9.715513  9.646146


$V44

 [1]  9.595969  8.790846 10.886678 10.862796 11.011536 11.121877  9.905381  9.105809

 [9]  8.273068  9.838949 11.949689 10.822118


$V45

 [1] 11.136599 10.048403  9.550736  9.187555  9.658043 10.445020  8.951360  9.791721

 [9] 10.311228 10.037643  9.793993  9.110682


$V46

 [1]  8.687501 11.147423  9.972880 10.066211 10.546771  9.062599 10.635468 10.232013

 [9] 11.345625  9.990024  9.991407 10.476954


$V47

 [1] 11.133615  9.795635 10.573759  9.865851 11.020739  9.984559  8.905284 10.082623

 [9]  9.308204  9.585267 10.483927  9.061792


$V48

 [1] 11.527897  9.306541  9.817261 10.011999  9.647924 11.488066  9.909529  9.189852

 [9] 10.384165  8.847272  9.021574  9.295149


$V49

 [1] 10.580548 10.410334  9.141755  9.951232  9.310824  9.530982  9.514146 10.353115

 [9]  9.172790  9.224184  9.246535 10.183254


$V50

 [1]  8.476810  9.866948 12.473140 11.096465  8.835443  8.791541 11.349148  9.490491

 [9] 11.766662 10.116328  8.698522  9.831709 

 > y

$V1

 [1]  4.015339  5.863456 10.908589 16.644048  3.586415 10.785211 15.952171 13.991881

 [9] -2.047566 14.037543 11.846166  4.204510


$V2

 [1] 11.5986050 11.1223394  4.3330926 11.9858945  3.0683900  0.4285652  5.6664773 11.0655365

 [9] 12.6441090 15.1575612  6.5604673 -6.8845891


$V3

 [1] 30.4858090 19.1903651 10.9587311  8.2432549 -2.1673356 19.6196732 21.0797542 12.1874175

 [9] 10.2624958 34.5674170 16.4012723  0.2746374


$V4

 [1]  6.054465 11.730736  9.178165  9.099751  5.286706 23.863680  5.763717  7.194635

 [9] -4.832662 15.415662 12.340102  5.348945


$V5

 [1]  9.682130 10.843301  7.659834  7.990530 -1.089217 17.949386 15.419049 13.359985

 [9] 15.980861  7.315910  5.625492  1.784369


$V6

 [1] 13.900792 -2.910538 -9.919566 11.619934 26.192403 11.005697 -8.028859 23.901832

 [9] 19.566231 11.123783 14.637008 22.947309


$V7

 [1] -13.794547  10.694331  -3.907253  10.221121   4.812186  30.336466  11.063367   5.713170

 [9]  10.906563   3.632984  19.062994  18.102237


$V8

 [1]  4.787456 -6.452299 23.702568  4.457065 11.418634 24.690993 10.892473  7.564364

 [9] 19.309754 12.026594 10.162080 -4.146879


$V9

 [1] 19.3681985  9.6962903  8.7613117 12.4783745  0.8971538  1.1631567  7.3246943 -4.2367523

 [9] 23.4928584 19.8231020  0.9434693 -5.7387309


$V10

 [1] -2.601023 10.375973  4.072120 -1.041408 19.124369  3.708078 16.514137 13.783774

 [9]  7.323494 22.486926  5.465823  5.393504


$V11

 [1] -0.2304685  6.3210024 10.9050876  0.9090195 12.8595414  1.8908955  0.1926135  9.5605253

 [9] 29.2307425  4.1334189 -6.1377370 -7.8801280


$V12

 [1]  20.93814  20.52897   8.46568  15.02737 -15.46326  36.19455  10.84032  23.75677

 [9]  11.99185  -1.80848  26.69742   3.16329


$V13

 [1] -8.444247 25.249788 -5.560716 14.011757 26.763728  9.060486  7.472202 21.207492

 [9]  6.790208 18.277567  1.312642 16.582461


$V14

 [1]  6.729204 33.362427 14.975839  9.025557 15.075072 -1.401448 10.169859 29.078613

 [9] 14.226905 17.111919 10.223668 18.205455


$V15

 [1] 21.429204  4.792800  1.191150  5.000242  8.330349 20.860509 11.472054 15.358397

 [9] 30.192352  5.099775 22.224800 12.528505


$V16

 [1] 25.3787078  8.8446262 19.8046995  8.3737244  3.0188198  0.5523626  1.7609299 10.8312218

 [9] 12.0730519  6.7727430  7.4409260  0.2973466


$V17

 [1]  6.0259098 -0.4754578 -8.1903543  0.1367535  5.4369173  6.0599242 10.8375439  2.3189462

 [9] 14.8567119  0.8194798  9.2698450 16.9552743


$V18

 [1] 15.742577  8.428059  2.246061 21.353946 16.992469 11.323058 23.668209  8.963437

 [9] 15.292405  4.366503 13.281732 22.160906


$V19

 [1]  4.611771 28.912725 24.868898  2.306133  8.950588 13.203968  1.262130  6.343924

 [9] 17.138957 11.822286 21.136370 -3.111609


$V20

 [1]  5.288301  8.695623  4.804431  4.555413 22.880971 10.912213 23.257241 -5.402096

 [9]  2.302810 22.612287 10.409988 18.802694


$V21

 [1] -6.2579628  4.1270881 15.8606239 -6.7518116  9.7867791  0.6013151  7.2975043  0.8923786

 [9] -1.8095508  0.8594594  8.6777588 -1.5276333


$V22

 [1] -0.5708967 13.4007552 15.7077921 11.3821379 13.9931414 24.5090773 20.8600907 -1.2247375

 [9] 12.3967146 -2.5243487  5.9970236 17.4002066


$V23

 [1] -0.42683043 24.85363078 11.18878137 10.49271625 12.82233140  0.06077157 18.80549356

 [8] 27.59220149  8.19065299 15.90782718 11.26396551 22.41196571


$V24

 [1] 32.2810974 23.9011709 11.8395955  9.4506041 27.1290118 18.9934731  0.2142779 -1.9449838

 [9] 15.4151122  0.3650899 13.8123986 -1.5743000


$V25

 [1] 10.577671 28.820320 17.885290  7.200444  7.233581 12.289848 25.572240  5.709248

 [9]  9.194635 15.066344 10.617179  6.084928


$V26

 [1] 11.1974971 -3.0508592 23.6032376  6.8238333  1.3555739  0.5371902  8.4139199  8.7051044

 [9] 23.6246937 -9.2808928  9.2700703  2.7369446


$V27

 [1] 30.54744537 21.49325127 21.63305774  5.47907273 14.33300899 28.94689691 14.25163854

 [8] -1.37666446 -0.37235904 -1.81846735 -0.03074614 -0.09997145


$V28

 [1]   0.53883912 -12.84096408   5.66896018  20.75801959   5.94356673  12.29496685

 [7]  12.94618446  12.48505381  13.22907720   3.63725538   8.94735937   0.09460342


$V29

 [1] 14.476896 14.038098 -6.286876 24.519986 16.396755 21.899095 16.508566 31.706405

 [9] -7.918906 -5.683970 10.341391  3.855630


$V30

 [1]  17.715335  11.791195  -2.083117   9.920530  10.919970   8.534892   9.867478  10.171304

 [9]  16.369495  19.970167 -10.453982   9.251442


$V31

 [1] 11.499897 20.544346  4.214543 13.938855  9.317015 13.061276 11.935922 33.982170

 [9] 18.017044  7.480648 17.093596 -9.650853


$V32

 [1] 15.581910 26.902214 13.590948 21.829779  6.648315  5.067585 -1.534243  5.706446

 [9] 23.454143  5.770701 11.523554 27.962010


$V33

 [1] 13.227351 -4.012569 25.896155 17.937642 -2.806699 13.902012 -1.292573  8.688711

 [9] 11.587781 11.193510  6.469587 21.794832


$V34

 [1] 19.4953104  5.9847500  0.4578837 13.6318392 22.6749570 24.8941531 10.6461320 22.6149657

 [9] -6.4390942 16.7477986 12.4009381  6.6352061


$V35

 [1] 16.334303  7.703884 11.908885 11.081741 22.987037 28.999834 17.358304 19.979099

 [9] 20.943119  4.879662  6.063903 -1.178533


$V36

 [1] 36.10414500 26.81472705  7.66482933 25.88972804  4.41133865 27.19378410 16.12016590

 [8] 10.16358777  5.30536966  0.08887342  7.04383230  6.66144651


$V37

 [1]  25.702518   8.706405  22.710031   5.939753   9.920586  13.883827  14.162927  11.574126

 [9]   3.587398 -14.062540  10.022890  -3.010528


$V38

 [1]  21.076250  12.379459   8.545778  15.622820  17.357709   3.149344  12.160667  14.323726

 [9]   9.287955 -12.775065   7.984152  19.870589


$V39

 [1]  4.084537  9.693435  8.857421 10.968986  5.266574 19.282237 14.664454  5.327032

 [9] 16.187388 14.718182 14.401287 -3.881021


$V40

 [1]  -3.594429  25.658788   5.943731  13.969313  13.128365  17.669600   7.319379  12.840996

 [9] -22.162300  -4.177205   1.485843   2.914564


$V41

 [1] 12.5215938  6.6157735  8.4115957 29.4366744 12.7680246 14.7404795 17.1023654 25.0834331

 [9] 10.7555553 12.2302676  0.6136142  6.6214011


$V42

 [1]  8.228548  4.653852 12.074326 -6.028216 -9.597958  1.542291 22.255133 -5.294395

 [9]  7.471434 19.912171 12.737261  8.036197


$V43

 [1]   7.930296  20.597610 -23.531257  11.051889  11.815925   2.729325  19.359914   7.841552

 [9]   0.266610  -2.986458  -1.040779   5.632739


$V44

 [1]  13.208630   4.151750   8.185622 -11.437754  25.911352  10.662108  17.971363  15.997452

 [9]   5.932255   5.357202  18.410557 -18.320663


$V45

 [1] 10.8752685 12.9495408 10.1223555 18.8015953  0.6473604 18.5797345  3.2061320 12.1588838

 [9] 14.7871615 14.9342139 -1.8500967 11.0448853


$V46

 [1] 19.268465 24.518172 32.417861 12.077448 -8.860945  4.503570 25.000839 15.574787

 [9] 12.216558 23.493042 30.192421 -4.288804


$V47

 [1]  3.105692  9.572717 19.784026 36.401833 21.147042 -6.880582  9.326402  6.731020

 [9] 10.579794  8.293651  1.755853 14.168061


$V48

 [1]  26.7365770  27.8233754   0.0390809  10.2331010   1.0773903 -10.5873595  -5.4538429

 [8] -14.7121663  15.2869994   8.4937058  16.3504856   3.4384350


$V49

 [1] 19.343466  7.007271 18.322945 28.596038  3.177361 15.723957 11.032964 21.648590

 [9] -5.787497 20.536615 15.388139  6.185372


$V50

 [1] -0.4029766 10.3815105  4.9058726 22.7198265 10.2211394 25.1476328  6.2523218 20.0850280

 [9] 17.9115040 24.8888563 25.5538003  1.0639196


extract p.value from results of the two-sample t-tests


- do student's t-test, which is two-sample t test assuming two groups has same variance, to test if the two groups have the same mean value or not for each pair of vectors in x,y.


> student.out = mapply(student.t.test, x, y)          


> student.out

         V1                   V2                       V3            V4                     V5             V6          V7 

0.563022081 0.144909548 0.108172703 0.519068519 0.701204745 0.773647965 0.715398189 

         V8                  V9                 V10                 V11             V12                     V13             V14 

0.863028497 0.427205171 0.557701219 0.092238646 0.411550907 0.776526786 0.156699263 

        V15                   V16                 V17                     V18             V19             V20             V21 

0.199343205 0.494011158 0.039083212 0.089387203 0.561065727 0.760924039 0.001237934 

        V22                 V23             V24                 V25                 V26                 V27         V28 

0.587839057 0.154551151 0.475907512 0.171426717 0.253470402 0.738496194 0.292089718 

        V29                 V30             V31                 V32                 V33                 V34         V35 

0.874044616 0.757006906 0.434279888 0.250515636 0.943289115 0.402992619 0.142474222 

        V36                 V37             V38                 V39                 V40                 V41         V42 

0.181966326 0.761334561 0.848560123 0.913061714 0.253963569 0.202944628 0.222228205 

        V43                 V44             V45                 V46                 V47                 V48         V49 

0.155193494 0.554371297 0.724748215 0.172569993 0.714074841 0.407738784 0.189612224 

        V50 

0.174348946 


> sum(student.out < 0.05)/600

[1] 0.003333333


do welch's t-test, which is two-sample t test assuming two groups has different variance, to test if the two groups have the same mean value or not for each pair of vectors in x,y.


> welch.out=mapply(welch.t.test,x,y)

> welch.out

         V1                         V2              V3              V4                      V5                  V6          V7 

0.568358090 0.158095795 0.121317301 0.525056819 0.704396849 0.776290911 0.718745087 

         V8                      V9                 V10                 V11                 V12                 V13         V14 

0.864512335 0.435463845 0.563436255 0.105464128 0.420255744 0.779130694 0.169579159 

        V15                 V16                     V17                 V18                 V 19             V20         V21 

0.212368216 0.500536645 0.050037938 0.102360810 0.566846588 0.763644531 0.003034717 

        V22                 V23                     V24                 V25                 V26         V27         V28 

0.592943775 0.167751387 0.483169957 0.184274339 0.265253913 0.741481401 0.303094093 

        V29                 V30                 V31                 V32                     V33         V34         V35 

0.875448707 0.759740310 0.442144866 0.262493876 0.943893212 0.411680263 0.155760662 

        V36                 V37                    V38             V39                     V40         V41         V42 

0.195201605 0.764146122 0.850234924 0.913935997 0.266036722 0.215418883 0.234836241 

        V43                 V44                     V45             V46                     V47         V48         V49 

0.168598836 0.560220106 0.727983598 0.186050504 0.717515047 0.416484219 0.202854879 

        V50 

0.187027273 


> sum(welch.out<0.05)/600

[1] 0.001666667

반응형