), 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 format:Lapply (X, fun, ...) The return value
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
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, executes the given function on each component o
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, executes the given function on each component o
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) {7RET 'even'8}Else9 {TenRET 'Odd' One } A return(ret) - } - the ##测试效果 -Func (35) - - ##sapply----The following code will error, the parameter can only be a numeric value +VEC ) - func (VEC) + ##这
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 operatio
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
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)
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 5.843333 3.057333 3.758000 1.19
vector should be returned to the matrix. Instead of using a function directly, the result is a one-dimensional vector that needs to be re-integrated using the matrix () function. But there is another way to do this, that is, the sapply () function (simplify apply abbreviation), the call format is sapply (x, f) input vector x applies the F () function to each of these elements and turns the result into a ma
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
find that the query results are different
SELECT u.id, COUNT(s.id) AS sapply, COUNT(uu.id) AS ftotal FROM A AS u RIGHT JOIN B AS s ON u.id = s.userid RIGHT JOIN C AS uu ON u.id = `uu`.`userid` GROUP BY `u`.`id` ORDER BY `ftotal` DESC LIMIT 10
The data is obviously problematic, and the results are separated.
SELECT u.id, COUNT(s.id) AS sapply FROM A AS u RIGHT JOIN B AS s ON u.id = s.userid GROUP BY `u`.`i
; 1.5 ) Any ( X> 1.5 ) # How to customize a function to calculate the circular area Myfunc Function ( R ) { Area Pi * R ^ 2 Return ( Area ) } Print ( Myfunc ( 4 ) ) # Calculate the area of four circles with different radius at the same time R C ( 2 , 2 , 4 , 3 ) Sapply ( X = r , Fun = myfunc ) # Project Euler 3 # Find the maximum prime factor of the number 600851475143 # Create a function to determine whether a certai
, B)
Arguments
Description
Cl
Cluster objects
Fun,fun
Function
X,x,a,b
Matrix
...
Additional parameters to pass to the fun
Moreargs
Additional parameters of fun
MARGIN
Specify the vector dimension to use
Simplify
logical value; see sapply
Use. NAMES
logical value; see
Summary ()Sapply (X,fun,options): statistics for each vector in a data frame or matrixMeanSD: Standard deviationvar: varianceMinMaxMedianLengthRangeQuantileVARs Head (Mtcars[vars])Summary (Mtcars[vars])Mystats if (na.omit)X M n S Skew Kurt return (c (n = n, mean = m, Stdev = s, skew = skew, kurtosis = Kurt))}Sapply (Mtcars[vars], mystats)#如结果, distribution Right (0.61), slightly flat than normal (-0.37)#Hmi
When you use the Unlist function to convert a list of date types to vectors, you do not get the desired result, as follows:Datelst datelst[[1]][1] "2015-08-11"datevector DATEVECTOR[1] 16658 The same reason for using sapply (X, fun, ..., simplify = TRUE, use. NAMES = TRUE), if X is a date-type vector, fun returns the date, then the result will not be the expected date-type vector, as follows:> date.vector.string > date.vector > date.vector20010101 11
distributed.T.test (y1,y2,parired=true) where Y1 and Y2 are two non-independent groups of numerical vectors> Library (MASS)> sapply (Uscrime[c ("U1", "U2")],function (x) (C (Mean=mean (x), SD=SD (x)))U1 U2Mean 95.46809 33.97872SD 18.02878 8.44545> With (uscrime,t.test (u1,u2,paired=true))Paired T-testDATA:U1 and U2t = 32.4066, df = P-value, Alternative hypothesis:true difference in means are not equal to 0Percent Confidence interval:57.67003 65.30870
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
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.