Leaf.R

aleph4 — Oct 30, 2014, 3:39 PM

library(dplyr)

Attaching package: 'dplyr'

The following object is masked from 'package:stats':

    filter

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
#Add ID variable to group observation
iris$id <- 1:150

head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species id
1          5.1         3.5          1.4         0.2  setosa  1
2          4.9         3.0          1.4         0.2  setosa  2
3          4.7         3.2          1.3         0.2  setosa  3
4          4.6         3.1          1.5         0.2  setosa  4
5          5.0         3.6          1.4         0.2  setosa  5
6          5.4         3.9          1.7         0.4  setosa  6
#Group observations by ID
grouped_iris <- group_by(iris, id)

grouped_iris
Source: local data frame [150 x 6]
Groups: id

   Sepal.Length Sepal.Width Petal.Length Petal.Width Species id
1           5.1         3.5          1.4         0.2  setosa  1
2           4.9         3.0          1.4         0.2  setosa  2
3           4.7         3.2          1.3         0.2  setosa  3
4           4.6         3.1          1.5         0.2  setosa  4
5           5.0         3.6          1.4         0.2  setosa  5
6           5.4         3.9          1.7         0.4  setosa  6
7           4.6         3.4          1.4         0.3  setosa  7
8           5.0         3.4          1.5         0.2  setosa  8
9           4.4         2.9          1.4         0.2  setosa  9
10          4.9         3.1          1.5         0.1  setosa 10
..          ...         ...          ...         ...     ... ..
#Mutate (aka transform) the dataframe to add a mean_all column
#Dont forget to save results into new dataframe
iris <- mutate(grouped_iris, mean_all = mean(c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width), na.rm=T))

iris
Source: local data frame [150 x 7]
Groups: id

   Sepal.Length Sepal.Width Petal.Length Petal.Width Species id mean_all
1           5.1         3.5          1.4         0.2  setosa  1    2.550
2           4.9         3.0          1.4         0.2  setosa  2    2.375
3           4.7         3.2          1.3         0.2  setosa  3    2.350
4           4.6         3.1          1.5         0.2  setosa  4    2.350
5           5.0         3.6          1.4         0.2  setosa  5    2.550
6           5.4         3.9          1.7         0.4  setosa  6    2.850
7           4.6         3.4          1.4         0.3  setosa  7    2.425
8           5.0         3.4          1.5         0.2  setosa  8    2.525
9           4.4         2.9          1.4         0.2  setosa  9    2.225
10          4.9         3.1          1.5         0.1  setosa 10    2.400
..          ...         ...          ...         ...     ... ..      ...
#It may not show the new variable because its too big but its in there

iris$mean_all
  [1] 2.550 2.375 2.350 2.350 2.550 2.850 2.425 2.525 2.225 2.400 2.700
 [12] 2.500 2.325 2.125 2.800 3.000 2.750 2.575 2.875 2.675 2.675 2.675
 [23] 2.350 2.650 2.575 2.450 2.600 2.600 2.550 2.425 2.425 2.675 2.725
 [34] 2.825 2.425 2.400 2.625 2.500 2.225 2.550 2.525 2.100 2.275 2.675
 [45] 2.800 2.375 2.675 2.350 2.675 2.475 4.075 3.900 4.100 3.275 3.850
 [56] 3.575 3.975 2.900 3.850 3.300 2.875 3.650 3.300 3.775 3.350 3.900
 [67] 3.650 3.400 3.600 3.275 3.925 3.550 3.800 3.700 3.725 3.850 3.950
 [78] 4.100 3.725 3.200 3.200 3.150 3.400 3.850 3.600 3.875 4.000 3.575
 [89] 3.500 3.325 3.425 3.775 3.400 2.900 3.450 3.525 3.525 3.675 2.925
[100] 3.475 4.525 3.875 4.525 4.150 4.375 4.825 3.400 4.575 4.200 4.850
[111] 4.200 4.075 4.350 3.800 4.025 4.300 4.200 5.100 4.875 3.675 4.525
[122] 3.825 4.800 3.925 4.450 4.550 3.900 3.950 4.225 4.400 4.550 5.025
[133] 4.250 3.925 3.925 4.775 4.425 4.200 3.900 4.375 4.450 4.350 3.875
[144] 4.550 4.550 4.300 3.925 4.175 4.325 3.950