You are on page 1of 2

Summarizing by Group: tapply

The tapply function takes three arguments


tapply(argument1, argument2, argument3) !

Group argument 1 by argument 2 and apply argument 3

To compute the average amount of iron, sorted by


high and low protein
tapply(USDA$Iron, USDA$HighProtein, mean, na.rm=TRUE) !

15.071x Understanding Food: Nutritional Education with Data

What exactly does tapply do?

Iron

HighProtein

0.27

12.8

1.44

0.38

4.50

2.29

HighProtein = 0

Food

HighProtein = 1

tapply(USDA$Iron, USDA$HighProtein, mean, na.rm=TRUE) !


Iron
12.8
1.44

Mean

2.29

Result
5.51
1.72

Iron
0.27
0.38

Mean

4.50

15.071x Understanding Food: Nutritional Education with Data

You might also like