sapply

Read about sapply, The latest news, videos, and discussion topics about sapply from alibabacloud.com

Introduction of Apply, Tapply, lapply, sapply, mapply, table and other functions in R

a factor when necessary. Example code: > FAC $ ' 2 ' [1] 57 $ ' 3 ' [1] 45 $ ' 4 ' NULL $ ' 5 ' NULL > Tapply (1:17, FAC, RANGE) $ ' 1 ' [1] 1 16 $ ' 2 ' [1] 2 17 $ ' 3 ' [1] 3 15 $ ' 4 ' NULL $ ' 5 ' NULL #利用tapply实现类似于excel里的数据透视表的功能: > da year Province Sale 12007 Nbsp;a1 22007 b2 32007c 3 42007d 4 52008a5 6 2008c6 72008 d7 82009 b8 92009 c9 2009d10 > attAch (DA) > Tapply (sale,list (year,province)) [1]147 102 8 1169 > Tapply (sale,list (year,province), mean) ab cd 200712 34 20085 NA 6 7 na

Use Apply, tapply, lapply, Sapply, mapply in R

), 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

Efficient batch processing functions in R (lapply sapply apply tapply mapply) _r language

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

R language Apply,sapply,lapply,tapply,vapply, the use of mapply

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

R in the use of apply, tapply, lapply, sapply, mapply, table and other functions for grouping statistics-reproduced

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

Basic operation of the R language-read and write txt,sapply&lapply

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) + ##这

R Language--tapply,sapply (actions of factors and lists)

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

Apply function in R language

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

R language Apply function family detailed

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)

Apply family functions and multithreading calculations in R

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

R Language Programming Art (2) data structures in R

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

Cyclic functions in the R language (Grouping function)

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

Mysql joins multiple tables when query results are out of the question

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

Stupid Way To Learn R programming (3)

; 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

An error occurred while querying multiple tables in Mysqljoin.

, `userid` int(11) default NULL, `dosomething` varchar(255) default NULL, PRIMARY KEY (`id`), KEY `userid` USING BTREE (`userid`)) ENGINE=MyISAM AUTO_INCREMENT=1; CREATE TABLE `C` ( `id` int(11) NOT NULL auto_increment, `userid` int(11) default NULL, `dosomething` varchar(255) default NULL, PRIMARY KEY (`id`), KEY `userid` USING BTREE (`userid`)) ENGINE=MyISAM AUTO_INCREMENT=1; Make your own attempt and find that the query results are different SELECT u.id, COUNT(s.id) AS

R Language Parallel Computing Snow package document (Beta)

, 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

R Language Practical reading notes (vii) BASIC statistical analysis

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

The exception that occurs when you use Unlist to convert a list of date data to vectors

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

R in Action reading notes (6)-Seventh chapter: Basic statistical analysis (Part I)

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

R language programming Art

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

Total Pages: 3 1 2 3 Go to: Go

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.