5.1 Abstraction

Source: Internet
Author: User

5.1 Abstraction

The main reason why R is a good thing is that it is a language. The charm of language is abstract. In the R language, functions are the way to implement abstractions. If we want to repeat the 1 to 3 integer two times. This is a simple command:

c(1:3, 1:3)

Now if you want to repeat these numbers six or 60 times. It makes sense to abstract this operation with a function. In fact, this abstraction has been done by predecessors:

rep(1:3, 6)  

Rep () can do our task above and some other similar tasks.

We are doing a new task. We have two vectors; we want to create a new vector, first repeating the first vector to the length of the second vector, The second vector is then repeated to the length of the first vector. If the length of a vector is less than its length, it means that only the part of the front of the vector is needed. Using the rep () function makes it easy to abstract it into a function:

repeat.xy <- function(x, y){ c(rep(x, length=length(y)), rep(y, length=length(x)))}

Repeat.xy () can now be used in R.

repeat.xy(1:4, 6:16)  

The easy way to write a function seems to mean that we can naturally upgrade from just using R to programming with R.

In addition to being abstract, the function also condenses wisdom. Pi is about 3.1415926535897932384626433832795028841971693993751058209749445.
923078 is wisdom.

Function:

Both wisdom and abstraction – it can help you figure out the area of any garden you want to know (about equal to).

This is not a place to talk purely about the structure of the R language, but a place to comment on the two function details we've talked about. The body of Repeat.xy () is surrounded by a pair of curly braces and Circle.area () No, No. The body of the function should be a simple expression. The curly braces convert an expression within the body into a separate (composite) expression. The curly braces are optional when the body of the function has only one command. Curly braces are also used in loops, branching, and judging structures.

Ideally, each function accomplishes a well-defined task by making it easy to understand the input and output. Beginners usually use a function to do all things. A generally better approach is to write a lot of small functions, Then there is a larger function called these small functions to accomplish everything. A step-by-step task splitting can give us a clear idea of what really should be done. And it makes debugging easier when a bug comes up. Use small functions to become more popular.

A gem of the R language abstraction is the default value of the function's entry parameter. For example, the default value for the NA.RM in SD () is false. If your demand is exactly false, you do not need to modify the value of NA.RM when you call SD (). If you want to throw away missing values, you're calling SD () When adding the parameter na.rm=true. If you write your own function and simply change the function's default entry, you may not appreciate the abstraction that the function gives you.

The function always returns a result to you, and the return value of the function almost proves its existence. The last statement in the function is defined as the return value. However, many functions do not obey such a mechanism, but return () will force the return you want to return.

Some other effect of the function is that it will have one or more negative effects. A negative effect is to change the code system in addition to returning results. The philosophy of R is to focus these negative actions on a few functions that we know clearly and want to affect the system (such as print (), plot (), RM ()).

The R function handles things that are objects. R has rich object types. Table 5.1 shows some important object types.

You may notice that each atom type has a possible value of –na (not Available), which is called the missing value , and some users of the first contact with R spend a lot of time trying to avoid the NAS. For the first time in 0, They might do the same. However, NA is a good thing that is very valuable to you. When you have missing values in your data, you're usually not happy, but living with Na is better than nothing.

The design ideal of R is important. Let's read "Nothing" again is important. The length of the vector can be 0. This is another stupid design but proved incredibly useful – That's not a stupid design. We don't often deal with a nonexistent thing, so in some cases this is a problem – we will see the example in Samsara 8, Samsara 8.1.15.

Most of the value of objects is in handling their properties. Many properties change the perception of R and the user. Most often, a property of an object will have its own name. The attribute that determines the direction of the object is a category . Table 5.2 Lists a few very important objects that are determined by attributes.

A common problem for beginners is to confuse data frames with matrices. They look the same. But they are really different. In Samsara 8.2.37 will show you why they differ.

"Vector" has many meanings in R:
1. An atomic object (as opposed to a list), which is perhaps the most common use.
2. An object without attributes (except for possible names). This is defined by the Is.vector () and As.vector () influence.
3. An object that can have any length (including a list).

It is obvious that the first definition and the third definition seem contradictory, and which of them belongs only through the Wencai of the code. When we discuss the difference between vectors and matrices, the second definition comes in handy.

The word "list" has a professional meaning in R – A scalable object that can contain different types, including its own length. Sometimes this word is also used in non-professional places, such as "Search list" or "argument list".

Not all functions are created equally. They can be randomly divided into three different types.

Below is a nameless function:

This function acts as the third parameter of apply (), which is so short that we do not need to name it.

These functions are used only on a specific occasion, and these are your disposable functions.

Yet for some of the functions that are truly precious to you. They are disposable and you rewrite them to make them more abstract. Admittedly, you might very well want a file or R package to introduce your precious useful functions.

From the example of the nameless function, we can see that a function can also be an argument to another function. In R, a function is like a vector or a matrix. You can think of a function as data.

an entirely new level of abstraction is the return value of a function is another function . The empirical distribution function is an example:

> mycumfun <- ecdf(rnorm(10))> mycumfun(0)[1] 0.4   

As long as you write a function that returns the code for another function, you can go straight to the next cycle.

In the second cycle (12 pages) We briefly analyzed Do.call (). Some people are very confused about this function. This is not necessary and unfortunate – in fact it is a very simple but very powerful function. In general, we call this function by the function name of the function and an "argument list". This is simple:

sample(x=10, size=5)  

The Do.call () function allows you to provide the arguments to a function with a real list:

do.call("sample", list(x=10, size=5))

It is sometimes useful to see specific executions when invoking a function. When a function is called, an environment is created, and when the called function calls another function, the system also creates its own environment for those other functions. So there is an environment stack in memory that scales as the program runs.

Let's define some functions:

ftop <- function(x){# time 1x1 <- f1(x)# time 5ans.top <- f2(x1)# time 9ans.top}f1 <- function(x){# time 2ans1 <- f1.1(x)# time 4ans1}f2 <- function(x){# time 6ans2 <- f2.1(x)# time 8ans2}

Then we make the call:

# time 0ftop(myx)# time 10  

Figure 5.1 shows us how the call changes in the context of the time stack. Note that in the environment of Ftop (), F1 (), F2 (), there is a x.x in Ftop () called MYX (or possibly a copy of x), as in F1 (). But the x in F2 () is a little different. .

When we debug the code, we'll look at the situation at a particular moment in the stack. For example, if a bug appears in f2.1, we're looking for a stack near time 7.

The R language has a rich object type. This is part of the benefits of R. Some of these objects are elements of the language itself – function calls, expressions, and so on. This provides a very powerful abstraction – in terms of language computing. Although almost all newcomers to the language of the elements of the confusion seems to be very confusing, However, many people are very insensitive to this view.

5.1 Abstraction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.