Изменения

Перейти к: навигация, поиск
м
Применение функций к нескольким переменным
<syntaxhighlight lang="rsplus">
> df DF <- data.frame(replicate(n = 10, rnorm(n = 100)))
</syntaxhighlight>
<syntaxhighlight lang="rsplus">
> > str(dfDF)
'data.frame': 100 obs. of 10 variables:
$ X1 : num 01.051 1.5448 08 -0.4478 477 -1.079 0396 3.0492 0.1193 423 ... $ X2 : num -0.322 0602 2.275 29 -0.103 758 -1.941 615 -0.318 364 ... $ X3 : num 0.58 0559 -01.558 0117 0.609 -5242 0.949 4105 -0.275 3191 ... $ X4 : num -0.6429 -10965 0.6286 2006 0.9507 29 0.0203 7702 -0.1642 0182 ... $ X5 : num -0.403 7074 -1.6111 0.913 -3478 0.716 -2.92 -2504 0.945 0609 ... $ X6 : num -1.59621 -432 0.00725 535 -0.70304 -932 0.41532 0581 -1.59548 606 ... $ X7 : num -0.671 1.074 42407 -0.136 31827 -2.04648 -0.628 19856 0.986 00301 ... $ X8 : num 0.2176 -511 0.0859 192 0.6535 467 -01.2412 0308 2.4143 496 ... $ X9 : num -0.412 18508 0.51 14481 -0.11 2828 -0.307 5464 0.647 0605 ... $ X10: num 01.436 -421 0.468 408 1.254 -0.399 956 -0.832 01.931 91 ...
</syntaxhighlight>
data: x
W = 0.98649903, p-value = 0.39776882
</syntaxhighlight>
> str(shapiro.test(x))
List of 4
$ statistic: Named num 0.98699
..- attr(*, "names")= chr "W"
$ p.value : num 0.398688
$ method : chr "Shapiro-Wilk normality test"
$ data.name: chr "x"
> normTest <- function (x) {
+ res <- shapiro.test(x)
+ return(clist(statistic = res$statistic, + p.value = res$p.value))
+ }
</syntaxhighlight>
<syntaxhighlight lang="rsplus">
> normTest(x)
$statistic W 0.9903 $p.value [1] 0.9863867 0.39771726882
</syntaxhighlight>
<syntaxhighlight lang="rsplus">
> t(sapply(dfDF, normTest)) W statistic p.valueX1 0.9860072 9831 0.374389222301 X2 0.9793961 9936 0.119275459213 X3 0.9961734 98 0.994706341333 X4 0.9836052 9829 0.250892072219 X5 0.9794991 9874 0.121504264625 X6 0.9895875 9862 0.631319353874 X7 0.9863415 9839 0.394883812617 X8 0.9849433 9833 0.31461676236 X9 0.9899328 9915 0.659163297834 X10 0.9757493 9808 0.061763141531
</syntaxhighlight>
<syntaxhighlight lang="rsplus">
> do.call(rbind, lapply(dfDF, normTest)) W statistic p.valueX1 0.9860072 9831 0.374389222301 X2 0.9793961 9936 0.119275459213 X3 0.9961734 98 0.994706341333 X4 0.9836052 9829 0.250892072219 X5 0.9794991 9874 0.121504264625 X6 0.9895875 9862 0.631319353874 X7 0.9863415 9839 0.394883812617 X8 0.9849433 9833 0.31461676236 X9 0.9899328 9915 0.659163297834 X10 0.9757493 9808 0.061763141531
</syntaxhighlight>

Навигация