0% found this document useful (0 votes)
12 views

Summarizing by Group: Tapply: The Tapply Function Takes Three Arguments

The tapply function takes three arguments and groups the first argument by the second argument to apply the third argument. It can be used to compute the average amount of iron sorted by high and low protein foods, by applying the mean function to the iron values after grouping them by the high protein variable. Running tapply on the USDA dataset's iron and high protein variables with the mean function calculates the average iron for foods with high and low protein.

Uploaded by

Shubham Vats
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Summarizing by Group: Tapply: The Tapply Function Takes Three Arguments

The tapply function takes three arguments and groups the first argument by the second argument to apply the third argument. It can be used to compute the average amount of iron sorted by high and low protein foods, by applying the mean function to the iron values after grouping them by the high protein variable. Running tapply on the USDA dataset's iron and high protein variables with the mean function calculates the average iron for foods with high and low protein.

Uploaded by

Shubham Vats
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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