Apply family functions and multithreading calculations in R

Source: Internet
Author: User

I. Apply family functions

1.apply applied to matrices and arrays

# apply # 1 for row, 2 for column # Create a matrix of ten rows x 2 columnsm <-matrix (C (1:10, 11:20), Nrow = ten, Ncol = 2) # mean of th E rowsapply (M, 1, mean) [1] 6 7 8 9 Ten 15# mean of the columnsapply (M, 2, mean) [1] 5.5 15.5# divide all values by 2apply (m, 1:2, function (x) X/2)

2.eapply variables applied to the environment

# a new Environmente <-new.env () # Environment variables, A and be$a <-1:10e$b <-11:20# mean of the VARIABL Eseapply (E, mean) $b [1] 15.5$a[1] 5.5

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 length

Sapply (Iris[,1:4],mean) sepal.length  sepal.width petal.length petal.width  5.843333     3.057333     3.758000     1.199333 lapply (Iris[,1:4],mean) $Sepal. length[1] 5.843333$sepal.width[1] 3.057333$petal.length[1] 3.758$PETAL.WIDTH[1] 1.199333

4.sapply is a friendly form of lapply. Both lapply and sapply can be applied to list,data.frame. Just return the object type is not the same, the former is a list, the latter is the case, if the element under each list is the same length, the returned result will be simplified. An example is described.

# The following two return results are the same, all listsapply (Iris,unique) lapply (iris,unique) # Two of the former return vectors, the latter return listsapply (Iris[,1:4],mean) lapply (Iris[,1:4],mean) #下面两个前者返回data. Frame, latter reversed listsapply (Iris[,1:4], function (x) X/2) lapply (Iris[,1:4], function (x) X/2 # sapply will choose the most appropriate object type to hold the object based on the return result, and list is list# the following two returns the same as the library (magrittr) lapply (Iris[,1:4],mean)%>%unlist () sapply (Iris[,1:4],mean)

5.vapply requires a third parameter, which is the format of the output

L <-List (a = 1:10, B = 11:20) # Fivenum of values using Vapplyl.fivenum <-vapply (L, Fivenum, C (min.=0, "1st Qu.") =0, Median=0, "3rd Qu." =0, max.=0)) class (L.fivenum) [1] "Matrix" # Let's see Itl.fivenum           a    bmin.     1.0 11.01st Qu.  3.0 13.0Median   5.5 15.53rd Qu.  8.0 18.0Max.    10.0 20.0

6.replicate
Description: "Replicate is a wrapper for the common use of sapply for repeated evaluation of an expression (which would usu Ally involve random number generation). "

Replicate (Rnorm (10))

7.mapply can pass multiple parameters in.

Mapply is a multivariate version of sapply. mapply applies fun to the first elements of each ... argument, the second elements, the third elements, an D so on. Arguments is recycled if necessary.

L1 <-List (a = C (1:10), B = C (11:20)) L2 <-List (c = C (21:30), d = C (31:40)) # sum the corresponding elements of L1 an D l2mapply (sum, l1$a, l1$b, L2$c, l2$d) [1]  96 100 #mapply像是可以传递多个参数的saplymapply (Rep, 1:4, 5) [, 1] [, 2] [, 3] [, 4][1,]    1    2    3    4[2,]    1    2    3    4[3,]    1    2    3    4[4,]    1    2    3    4[5,]    1    2    3    4

8.rapply
Description: "Rapply is a recursive version of Lapply."

# let's start with our usual simple list examplel <-list (a = 1:10, B = 11:20) # log2 of each value in the Listrapply (L, log2) A1 A2 A3 A4 A5 A6 A7 A8 0.000000 1       .000000 1.584963 2.000000 2.321928 2.584963 2.807355 3.000000 A9 A10 B1 B2 B3 B4 B5 b6 3.169925 3.321928 3.459432 3.584963 3.700440 3.807355 3.906891 4.000000 B7 B8 b9 B10 4. 087463 4.169925 4.247928 4.321928# log2 of each value in each listrapply (l, log2, how = "list") $a [1] 0.000000 1.000000 1. 584963 2.000000 2.321928 2.584963 2.807355 3.000000 [9] 3.169925 3.321928 $b [1] 3.459432 3.584963 3.700440 3.807355 3.906  891 4.000000 4.087463 4.169925 [9] 4.247928 4.321928 # What if the function is the mean?rapply (l, mean) a B 5.5 15.5 Rapply (l, mean, how = "list") $a [1] 5.5 $b [1] 15.5 

Two. Multithreaded computing

The following Euler problem 14 is used to demonstrate Vectorization programming in R (using the Apply Group function) and multithreading

#-----Longest Collatz sequence problem 14func <-function (x) {    n = 1    raw <-x while    (x > 1) {        X & lt;-IfElse (x%%2==0,x/2,3*x+1)        n = n + 1    }    return (c (raw,n))} #方法1 vectorization Program library (magrittr) system.time ({    x <-1:1e5    res1 <-sapply (x, func)%>%t ()}) User   system   elapsed 37.960  0.360 41.315# Method 2 Vectorization programming System.time ({    x <-1:1e5    res2 <-do.call (' Rbind ', lapply (X,func))}) User   system   elapsed 36.031  0.181 36.769# Method 3 multi-Threading Compute Library (parallel) # Use System.time to return the time required for the calculation system.time ({    x <-1:1e5    cl <- Makecluster (4) # initializes    a parallel version of the Quad core cluster results <-parlapply (cl,x,func) # lapply res.df <-do.call    (' Rbind ', Results) # Consolidation result    Stopcluster (CL) # Close cluster}) User   system   goes 0.199  0.064 20.038 # Method 4 for Loop system.time ({    m <-matrix (nrow = 0,ncol = 2) for    (i in 1:1e5) {        m <-rbind (M,func (i))    }}) #方法4用时太长

Above.

Reference:

A Brief introduction to ' Apply ' in R

Playing parallel computing with parallel and foreach packets

Apply family functions and multithreading calculations in R

Related Article

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.