Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 259

7.3.5 Conclusion

Оглавление

When you come from a background of compiled languages that provides fine graded control over memory management (such as C or C++), you might not directly see the need for pipes that much. However, it does reduce the amount of text that needs to be typed and makes the code more readable.

Indeed, the piping operator will not provide a speed increase nor memory advantage even if we would create a new variable at every line. R has a pretty good memory management and it does only copy columns when they are really modified. For example, have a careful look at the following:

library(pryr) x <- runif(100) object_size(x) ## 840 B y <- x # x and y together do not take more memory than only x. object_size(x,y) ## 840 B y <- y * 2 # Now, they are different and are stored separately in memory. object_size(x,y) ## 1.68 kB

The piping operator can be confusing at first and is not really necessary (unless to read code that is using it). However, it has the advantage to make code more readable – once used to it – and it also makes code shorter. Finally, it allows the reader of the code to focus more on what is going on (the actions instead of the data, since that is passed over invisibly).

The Big R-Book

Подняться наверх