The R language provides a bulk processing function that iterates through all or part of the elements within a collection to simplify operations.
The bottom of these functions is achieved by C, so efficiency is more efficient than manual.The batch processing function has very important apply family function: lapply sapply apply tapply mapply. The Apply family function is one of the methods to realize the computational quantization (vectorization) with
), Usena = C ("No", "Ifany", "Always"), DNN = List.names (...), Deparse. Level = 1) where the parameter exclude indicates which factors are not counted.Example code:> D > D[1] A b c A b c a b c a b c a b c a b c a b c a b c a b c a B CLevels:a B C D E> table (d)DA B C D E10 10 10) 0 0> table (d, exclude= "B")DA C D E10 10 0 0
function lapply and function sapply:
Lapply is used in the following
Apply ()Apply (M,dimcode,f,fargs)
M is a matrix.
DimCode is the dimension number, 1 is applied to the row, and 2 is the function for the column.
F is a function
Fargs is an optional parameter set for F
> z > f + x/c(2, 8)+ }> apply(z,1,f) #f函数得到两个元素,则为几行,竖着来的 [,1] [,2] [,3][1,] 0.5 1.000 1.50[2,] 0.5 0.625 0.75Lapply ()Lapply () (on behalf of list apply) is similar to the use of the Apply () function of the matrix, ex
Apply ()Apply (M,dimcode,f,fargs)
M is a matrix.
DimCode is the dimension number, 1 is applied to the row, and 2 is the function for the column.
F is a function
Fargs is an optional parameter set for F
> z > f + x/c(2, 8)+ }> apply(z,1,f) #f函数得到两个元素,则为几行,竖着来的 [,1] [,2] [,3][1,] 0.5 1.000 1.50[2,] 0.5 0.625 0.75Lapply ()Lapply () (on behalf of list apply) is similar to the use of the Apply () function of the matrix, ex
More busy than busy more recently ... Really busy///As a rookie, do not understand why so easy to forget the basic operation of R, feel more difficult than C # JAVA, and to review again1 ##数据操作的入门2 3 ##定义一个函数判断偶数4Func function (x)5 {6 if(x% 2 = =0)
ObjectiveAt the beginning of the contact with the R language, you will hear a variety of R language use skills, the most important one is not to use the loop, the efficiency is particularly low, to use vector computing instead of cyclic calculation.So, what is this for? The reason is that R's cyclic operation for and while, are based on the R language itself, and the vector operation is based on the underlying C language function implementation, from the performance point of view, there will be
Looping on the Command lineWriting for, while loops was useful when programming and not particularly easy when working interactively on the command Li Ne. There is some functions which implement looping to make life easierlapply: Loop over a list and evaluate a function on each elementsapply: Same as Lapply but try to Simpli FY the resultApply: Apply a function over the margins of an arraytapply: Apply a function over subsets of a vector mapply:multiv
I. Apply family functions1.apply applied to matrices and arrays# apply # 1 for row, 2 for column # Create a matrix of ten rows x 2 columnsm 2.eapply variables applied to the environment# a new Environmente 3.lapply applies to the list, returns the list, and the actual data.frame is also a list, a list:lapply (list, function) cbind together by multiple vectors of the same lengthSapply (Iris[,1:4],mean) sepal.length sepal.width petal.length petal.width
Apply
Apply Functions over Array MarginsUse functions against column rows or columns
Apply (X, MARGIN, fun, ...)
Lapply
Apply a Function over a List or VectorUsing functions on lists or vectors
Lapply (X, fun, ...)
Sapply
Apply a Function over a List or VectorUsing functions on lists or vectors
Sapply (X, fun, ..., simplify = TRUE, use.) NAMES = TRUE)
There are several commonly used functions in the R language, which can be processed by group, apply, Lapply, sapply, tapply, mapply, etc. These functions are somewhat similar, and the following describes the usage of these functions.ApplyThis is an operation on a matrix or array for a dimension. The format is: Apply (data, dimension index, arithmetic function, function parameter) For the matrix, the dimension value is 2, the second parameter dimension
index: Note the following code:#提取列表组件三种方法j1 To add or remove a list element:Add list elements: Use the index directly to increase the list components, there are 5 ways to see the code above, you can add a single component, or you can add a sub-list as a list of multiple components.Delete List element: Assigns the component to be deleted directly to NULL. Note that when you delete an intermediate component, the index of the subsequent component is all minus 1Get list Length:The length () func
cachemean execution? Getmean#This copies the M to Getmean (NULL)List (set=set,get=GET, Setmean=Setmean, Getmean=Getmean)} Cachemeanfunction (x, ...) {m#first assign the Getmean to M, and if M is not NULL, the following hint pops up if(! is. Null (M)) {Message ("Getting cached data") return(M)}#Cache value appearsData#assigns the first numberic vector to the datammean (data, ...) X$setmean (m) cache M-m}AboveIn addition, several useful functions were also discussed this wee
columns of the matrix: look for an example of changing the size of a matrix 613.4.2 by adding or deleting a matrix row or column 613.4.1 with an abnormal value 603.4: find the difference between the closest 633.5 vector and the matrix of an endpoint in the graph. 653.6 avoid unexpected dimensionality reduction. 663.7 naming of rows and columns of the 683.8 matrix. 4th high-dimensional array. 68 chapter 714.1 list. 714.2 create list general Operation 724.2.1 List Index 724.2.2 add or delete list
repeated calculation results
In the simplest condition group, the condition does not overlap, but in fact it is very common to overlap. For example, grouping order amounts according to the following rules:
1000 to 4000: regular order R14
Less than 2000: Non-key order r2
More than 3000: Key orders r3
The regular orders here will overlap with the other two groups. In this case, whether or not to repeat the overlapped data is required.
Code:
R14
R2
R3
Grouped
Result
Calculation Result:
650) T
Recently participated in a small game where the tapply,sapply (lapply) function can quickly achieve my purpose and effectively reduce the amount of code
F
Description
Tapply (X,F,G)
X is a vector, F is a factor column, G is an action function, and a similar operation with a data frame can be done by using the by function
Sapply (LIST,G)
G is the operation function, the return result is a vector, an
operations at the same time, usually by executing the same or a batch of instructions on different data, or applying an instruction to an array/vector, compared to the way the general program performs only one operation at a time. The following list of several vectorization operations used frequently in R is a trivial operation, but they are essentially the same operations applied to a batch of data at the same time, so they can be transformed by a vectorization approach:Vector values, such as:
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.