Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- Basic R by Roberto de Pinho 399 views
- R Programming: Mathematical Functio... by Rsquared Academy 514 views
- R squared project by Eduardo García 1522 views
- 20100929 ggplot - triangle useRs gr... by eballen01 1774 views
- Introduction2R by Aureliano Bombare... 686 views
- Q plot tutorial by Abhik Seal 889 views

12,036 views

11,825 views

11,825 views

Published on

R: Apply Functions

No Downloads

Total views

12,036

On SlideShare

0

From Embeds

0

Number of Embeds

57

Shares

0

Downloads

0

Comments

0

Likes

1

No embeds

No notes for slide

- 1. Apply functions<br />The Dataminingtools.net Team<br />
- 2. Apply functions<br />Apply functions are used to execute a function repetitively. "Apply" functions keeps us from having to write loops to perform some operation on every row or every column of a matrix or data frame, or on every element in a list.<br />
- 3. Apply family<br />sapply()<br />lapply()<br />apply()<br /><ul><li>mapply()
- 4. tapply()
- 5. rapply()</li></li></ul><li>Usage<br />Using Loops!<br />> avg <- numeric (8)<br />> avg<br />[1] 0 0 0 0 0 0 0 0<br />> for(i in 1:8)<br />+ avg[i]<-mean(state.x77[,i])<br />> avg[i]<br />[1] 70735.88<br />> avg<br />[1] 4246.4200 4435.8000 1.1700 70.8786 7.3780<br />[6] 53.1080 104.4600 70735.8800<br />
- 6. Usage<br />Using ‘apply’<br />> apply (state.x77, 2, median)<br />Population Income Illiteracy Life Exp Murder <br /> 2838.500 4519.000 0.950 70.675 6.850 <br /> HS Grad Frost Area <br /> 53.250 114.500 54277.000<br />The 2 means "go by column" -- a 1 would have meant "go by row."<br />
- 7. Usage<br />We construct a function and pass it to apply. It computes the median and maximum of each column of state.x77.<br />
- 8. Usage<br /> apply() works on each row, one at a time, to find the smallest number in each row. which() function, returns the indices within a vector for which the vector holds the value TRUE<br />
- 9. lapplyand sapply<br />The lapply() function works on any list. The "l" in "lapply" stands for "list."<br />The "s" in "sapply" stands for "simplify."<br />
- 10. tapply<br />tapply() is a very powerful function that lets us break a vector into pieces and apply some function to each of the pieces. It is like sapply(), except that with sapply() the pieces are always elements of a list. With tapply() we get to specify how the breakdown is done. <br />>tapply(barley$yield, barley$site, mean) <br />Grand Rapids Duluth University Farm Morris Crookston Waseca <br />24.93167 27.99667 32.66667 35.4 37.42 48.10833<br />

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment