Loop functions


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »


View on screencast.com »

 

> str(iris)'data.frame':        150 obs. of  5 variables: $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...> tapply(iris$Sepal.Length, iris$Species, mean)    setosa versicolor  virginica      5.006      5.936      6.588 > apply(iris[, 1:4], 2, mean)Sepal.Length  Sepal.Width Petal.Length  Petal.Width     5.843333     3.057333     3.758000     1.199333> str(mtcars)'data.frame':        32 obs. of  11 variables: $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... $ cyl : num  6 6 4 6 8 6 8 4 4 6 ... $ disp: num  160 160 108 258 360 ... $ hp  : num  110 110 93 110 175 105 245 62 95 123 ... $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ... $ wt  : num  2.62 2.88 2.32 3.21 3.44 ... $ qsec: num  16.5 17 18.6 19.4 17 ... $ vs  : num  0 0 1 1 0 1 0 1 1 1 ... $ am  : num  1 1 1 0 0 0 0 0 0 0 ... $ gear: num  4 4 4 3 3 3 3 4 4 4 ... $ carb: num  4 4 1 1 2 1 4 2 2 4 ...> apply(mtcars, 2, mean)       mpg        cyl       disp         hp       drat         wt  20.090625   6.187500 230.721875 146.687500   3.596563   3.217250       qsec         vs         am       gear       carb  17.848750   0.437500   0.406250   3.687500   2.812500 > split(mtcars$mpg, mtcars$cyl)$`4` [1] 22.8 24.4 22.8 32.4 30.4 33.9 21.5 27.3 26.0 30.4 21.4$`6`[1] 21.0 21.0 21.4 18.1 19.2 17.8 19.7$`8` [1] 18.7 14.3 16.4 17.3 15.2 10.4 10.4 14.7 15.5 15.2 13.3 19.2[13] 15.8 15.0> sapply(split(mtcars$mpg, mtcars$cyl), mean)       4        6        8 26.66364 19.74286 15.10000 > tapply(mtcars$mpg, mtcars$cyl, mean)       4        6        8 26.66364 19.74286 15.10000

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s