• Sonuç bulunamadı

7.4 Functions Writing functions in

N/A
N/A
Protected

Academic year: 2021

Share "7.4 Functions Writing functions in"

Copied!
2
0
0

Yükleniyor.... (view fulltext now)

Tam metin

(1)

7.4

Functions

Writing functions inRis very easy. Whenever you want to run certain code over and over again, but don’t want to source or paste things into the console repeatedly, you can and should create your own function with thefunction(). Functions take objects as arguments and return another object as output. Consider the following function:

1 > MyFunction <- function( Object ){

Object + Object } 2 > MyFunction function ( Object ){ Object + Object } 3 > MyFunction ( Object = 17) [1] 34

You can wrap all sorts of functions, loops, and statements inside of a function to simplify repetitive tasks. You can specify as many parameters as you like. Let’s make a function that makes a basic scatterplot. Note: therequire()function will automatically load theggplotpackage if needed.

1 > Scatterplot <- function(X, Y, Title , X.Axis , Y. Axis ) {

require( ggplot2 )

temp <- data.frame(X, Y)

limits .X <- c(min(X) - 0.25*sd(X), max(X) + 0.25*sd(X)) limits .Y <- c(min(Y) - 0.25*sd(Y), max(Y) + 0.25*sd(Y)) ggplot (data = temp ) +

geom_point (aes(x = X, y = Y), alpha = 0.3) + labs (title = Title , x = X.Axis , y = Y. Axis ) +

coord_cartesian ( xlim = limits .X , ylim = limits .Y) }

>

Let’s take our function for a test drive. We can use the fuel economy data. 1 > Var1 <- FE2013$FEhighway

2 > Var2 <- FE2013$FEcity

3 > Scatterplot (X = Var1 , Y = Var2 , Title =" Fuel Economy ", X.

Axis = " Highway ", Y. Axis = " City ") >

(2)

10 20 30 40 50 20 30 40

Highway

City

Fuel Economy

79

Referanslar

Benzer Belgeler

Eating regularly is good for your blood sugar levels, your nutrition and your weight.. It

Try halving the sweet treats and doubling the amount of fruit and vegetables.. Start with one extra piece of fruit or a few more vegetables with

kategorisi, eylemin gerçekleĢtirdiği (modalite) asıl unsurudur. Bunun dıĢında daha fazla kip vardır. Bunlar bildirme, Ģart, istek, emir, admirative ve dilek

Your references should include your primary source(s) (that is the original texts that you are planning to analyze) and at least 10 secondary sources (articles, books, collections

You illustrate your research in developing your ideas, in displaying the objectivity of your ideas and above all in demonstrating that you are aware of different opinions,

Figure 1: The number of bitrate switches, stalls, the startup delay in seconds (left), and the underutilization and instability indexes [20] (right) of a DASH player with three

Since the properties (uniqueness and continuous dependence on the data) are satis…ed, Laplace’s equation with u speci…ed on the boundary is a

(b) You play the game only once, as above, but, this time you must announce your decision before your competitor does.. Will you choose the