Blog posts on Data Science, Machine Learning, Data Mining, Artificial Intelligence, Spark Machine Learning

Friday, March 18, 2016

apply lapply rapply sapply functions in R

As part of Data Science with R, this is third tutorial after basic data types,control structures in r.

One of the issues with for loop is its memory consumption and its slowness in executing a repetitive task at hand. Often dealing with large data and iterating it, for loop is not advised. R provides many few alternatives to be applied on vectors for looping operations. In this section, we deal with apply function and its variants:


?apply


Datasets for apply family tutorial
 For understanding the apply functions in R we use,the data from 1974 Motor Trend
US magazine which comprises fuel consumption and 10 aspects of automobile design and
performance for 32 automobiles (1973–74 models).
 
data("mtcars")
head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
 

Reynolds (1994) describes a small part of a study of the long-term temperature dynamics
of beaver Castor canadensis in north-central Wisconsin. Body temperature was measured by
telemetry every 10 minutes for four females, but data from a one period of less than a 
day for each of two animals is used there. 

data(beavers)
head(t(beaver1)[1:4,1:10])
        [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]    [,9]   [,10]
day   346.00 346.00 346.00 346.00 346.00 346.00 346.00 346.00  346.00  346.00
time  840.00 850.00 900.00 910.00 920.00 930.00 940.00 950.00 1000.00 1010.00
temp   36.33  36.34  36.35  36.42  36.55  36.69  36.71  36.75   36.81   36.88
activ   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00    0.00    0.00

apply():
apply() function is the base function. We will learn how to apply family functions by trying out the code. apply() function takes 3 arguments:
  • data matrix
  • row/column operation, - 1 for row wise operation, 2 for column wise operation
  • function to be applied on the data.
 
when 1 is passed as second parameter, the function max is applied row wise and gives
us the result. In the below example, row wise maximum value is calculated.Since we 
have four types of attributes we got 4 results.
 
apply(t(beaver1),1,max) 
    day    time    temp   activ 
 347.00 2350.00   37.53    1.00 

 
When 2 is passed as second  parameter the function  mean is applied column wise.
In the below example mean function is applied on each column and mean for each 
column is calculated. Hence  we can see results for each column.
 
apply(mtcars,2,mean) 
       mpg        cyl       disp         hp       drat         wt       qsec         vs         am       gear       carb 
 20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750   0.437500   0.406250   3.687500   2.812500 
 
We can also pass custom function instead of default functions. For example in 
the below example let us divide each column element with modulus of 10.
For this we use a custom function which takes each element from each column and
apply the modulus operation.
 
head(apply(mtcars,2,function(x) x%%10))
                  mpg cyl disp hp drat    wt qsec vs am gear carb
Mazda RX4         1.0   6    0  0 3.90 2.620 6.46  0  1    4    4
Mazda RX4 Wag     1.0   6    0  0 3.90 2.875 7.02  0  1    4    4
Datsun 710        2.8   4    8  3 3.85 2.320 8.61  1  1    4    1
Hornet 4 Drive    1.4   6    8  0 3.08 3.215 9.44  1  0    3    1
Hornet Sportabout 8.7   8    0  5 3.15 3.440 7.02  0  0    3    2
Valiant           8.1   6    5  5 2.76 3.460 0.22  1  0    3    1

lapply():
lapply function is applied for operations on list objects and returns a list object of same length of original set.
lapply function in R, returns a list of the same length as input list object, each element of which is the result of applying FUN to the corresponding element of list.
 #create a list with 2 elements 
l = (a=1:10,b=11:20)  # the mean of the value in each element
lapply(l, mean)
$a
[1] 5.5
$b
[1] 15.5
class(lapply(l, mean))
[1] "list
  # the sum of the values in each element 
lapply(l, sum)
$a
[1] 55

$b
[1] 155



sapply():
sapply is wrapper class to lapply with difference being it returns vector or matrix instead of list object.
 
 # create a list with 2 elements 
 l = (a=1:10,b=11:20)  # mean of values using sapply 
sapply(l, mean)
   a    b 
 5.5 15.5

tapply():
tapply() is a very powerful function that lets you break a vector into pieces and then apply some function to each of the pieces. In the below code, first each of mpg in mtcars data is grouped by cylinder type and then mean() function is calculated.
str(mtcars$cyl)
 num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
levels(as.factor(mtcars$cyl))
[1] "4" "6" "8"

In the dataset we have 3 types of cylinders and now we want to see the average mpg
for each cylinder type.

tapply(mtcars$mpg,mtcars$cyl,mean)
       4        6        8 
26.66364 19.74286 15.10000 

In the output above we see that the average mpg for 4 cylinder engine 
is 26.664, 6-cyinder engine is 19.74 and 8-cylinder engine is 15.10

by():
by works similar to group by function in SQL, applied to factors, where in we may apply operations on individual results set. In the below example, we apply colMeans() function to all the observations on iris dataset grouped by Species.
data(iris) 
'data.frame': 150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

by(iris[,1:4],iris$Species,colMeans)
iris$Species: setosa
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
       5.006        3.428        1.462        0.246 
------------------------------------------------------------------------------------ 
iris$Species: versicolor
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
       5.936        2.770        4.260        1.326 
------------------------------------------------------------------------------------ 
iris$Species: virginica
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
       6.588        2.974        5.552        2.026 

rapply():
rapply() is a recursive version of lapply.
rapply() applies a function recursively on each element of the list with 2 modes for "how" parameter. If how = "replace", each element of the list which is not itself a list and has a class included in classes is replaced by the result of applying f to the element.If the mode is how = "list" or how = "unlist", the list is copied, all non-list elements which have a class included in classes are replaced by the result of applying f to the element and all others are replaced by deflt. Finally, if how = "unlist", unlist(recursive = TRUE) is called on the result.
'
l2 = list(a = 1:10, b = 11:20,c=c('d','a','t','a'))
l2
$a
 [1]  1  2  3  4  5  6  7  8  9 10

$b
 [1] 11 12 13 14 15 16 17 18 19 20

$c
[1] "d" "a" "t" "a"

rapply(l2, mean, how = "list", classes = "integer")
$a
[1] 5.5

$b
[1] 15.5

$c
NULL

rapply(l2, mean, how = "unlist", classes = "integer")
 a    b 
 5.5 15.5 
 
rapply(l2, mean, how = "replace", classes = "integer")
$a
[1] 5.5

$b
[1] 15.5

$c
[1] "d" "a" "t" "a"

mapply():
mapply is a multivariate version of sapply. By R definition, mapply is a multivariate version of sapply. mapply applies FUN to the first elements of each ... argument, the second elements, the third elements, and so on. Arguments are recycled if necessary. Its purpose is to be able to vectorize arguments to a function that is not usually accepting vectors as arguments. In short, mapply applies a Function to Multiple List or multiple Vector Arguments. In the below example word function is applied to vector argument LETTERS. '
word = function(C, k) paste(rep.int(C, k), collapse = "")
utils::str(mapply(word, LETTERS[1:6], 6:1, SIMPLIFY = FALSE))
List of 6
 $ A: chr "AAAAAA"
 $ B: chr "BBBBB"
 $ C: chr "CCCC"
 $ D: chr "DDD"
 $ E: chr "EE"
 $ F: chr "F"
 

35 comments:

  1. Yet, thank you for this excellent point and although I do not necessarily go along with the idea in totality, I regard your perspective online marketing agency

    ReplyDelete
  2. It's a shame you don't have a donate button! I'd most certainly donate to this brilliant blog! I guess for now i'll settle for bookmarking and adding your RSS feed to my Google account. I look forward to brand new updates and will share this blog with my Facebook group. Chat soon!
    professional painter Singapore

    ReplyDelete
  3. I was curious if you ever thought of changing the page layout of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or two pictures. Maybe you could space it out better?
    e commerce singapore statistics

    ReplyDelete
  4. I loved as much as you'll receive carried out right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield this increase. komodo liveaboard

    ReplyDelete
  5. F*ckin’ awesome things here. I am very glad to see your post. Thanks a lot and i'm looking forward to contact you. Will you please drop me a e-mail? Content strategy

    ReplyDelete
  6. Good day! I know this is somewhat off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having problems finding one? Thanks a lot! fundraising in Singapore

    ReplyDelete
  7. Greetings from Colorado! I'm bored at work so I decided to browse your site on my iphone during lunch break. I enjoy the information you present here and can't wait to take a look when I get home. I'm shocked at how fast your blog loaded on my mobile .. I'm not even using WIFI, just 3G .. Anyways, amazing blog!
    flyer printing and brochure printing services 

    ReplyDelete
  8. Thanks for the auspicious writeup. It in truth used to be a leisure account it. Glance complex to more delivered agreeable from you! However, how can we keep up a correspondence? singapore advertisement

    ReplyDelete
  9. Hi, i think that i saw you visited my web site thus i came to “return the favor”.I am attempting to find things to improve my site!I suppose its ok to use a few of your ideas!! Why You Need A Search Engine Optimization Expert

    ReplyDelete
  10. There is a mistake in this piece of code l = (a=1:10,b=11:20), I believe it should be l=list(a=1:10, b=11:20). very useful.

    ReplyDelete
  11. Nice read, I just passed this onto a colleague who was doing a little research on that. And he just bought me lunch as I found it for him smile Therefore let me rephrase that: Thanks for lunch! Copper Tube

    ReplyDelete
  12. Hey there! Do you know if they make any plugins to help with Search Engine Optimization? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good results. If you know of any please share. Kudos! List of Chemical Companies in Singapore

    ReplyDelete
  13. Beautiful illustration of those concepts..

    ReplyDelete
  14. Wonderful blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Appreciate it. relex smile singapore

    ReplyDelete
  15. This design is spectacular! You most certainly know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost...HaHa!) Great job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool! Stretch mark removal Singapore

    ReplyDelete
  16. Hey would you mind sharing which blog platform you're working with? I'm planning to start my own blog soon but I'm having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I'm looking for something unique.   accounting services Singapore

    ReplyDelete
  17. Fascinating blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog stand out. Please let me know where you got your design. Bless you forex exchange

    ReplyDelete
  18. I and also my buddies ended up going through the best thoughts on your web blog and so immediately I had a horrible feeling I had not thanked the website owner for those strategies. The boys happened to be consequently passionate to read through all of them and now have surely been taking advantage of these things. Appreciate your really being very helpful and for selecting certain impressive resources most people are really desperate to be informed on. My personal sincere regret for not expressing gratitude to earlier.leadership and management courses

    ReplyDelete
  19. This is something I need to learn about before even starting working.
    buy logo design

    ReplyDelete
  20. Fascinating article thank you for sharing them, I trust you will keep on having comparative presents to share with everybody. Also, we deliver extended support on SBCglobal Pop Settings, if while accessing your account you are seeing unnecessary pop-up then, you can read more information visit our website. SBCglobal Pop Settings

    ReplyDelete
  21. Hey! Someone in my Myspace group shared this site with us so I came to check it out. I'm definitely loving the information. I'm bookmarking and will be tweeting this to my followers! Exceptional blog and fantastic style and design.space rental singapore

    ReplyDelete
  22. Thanks for sharing
    Village Talkies a top-quality professional corporate video production company in Bangalore and also best explainer video company in Bangalore & animation video makers in Bangalore, Chennai, India & Maryland, Baltimore, USA provides Corporate & Brand films, Promotional, Marketing videos & Training videos, Product demo videos, Employee videos, Product video explainers, eLearning videos, 2d Animation, 3d Animation, Motion Graphics, Whiteboard Explainer videos Client Testimonial Videos, Video Presentation and more for all start-ups, industries, and corporate companies. From scripting to corporate video production services, explainer & 3d, 2d animation video production , our solutions are customized to your budget, timeline, and to meet the company goals and objectives.
    As a best video production company in Bangalore, we produce quality and creative videos to our clients.

    ReplyDelete
  23. Great article... Neem oil is a naturally occurring pesticide found in seeds from the neem tree. It is yellow to brown, has a bitter taste, and a garlic/sulfur smell.
    Order organic neem oil online at: https://plantneeds.com.au/product/planc

    ReplyDelete
  24. Thank you for your valuable and useful information through the blog. I am appreciating with the way you shared the relevant, precious, and perfect information. Furthermore, I would like to also keep some sound knowledge of TomTom GPS Update . If you have interest in Tom Tom Maps so just visit our website and you will get complete Assistance related to Maps Updates and send me your valuable feedback on our website. I hope, you will get immediate assistance and right information through our website.

    ReplyDelete
  25. Informative content. wanted to know what is the next national day and how it is celebrated? The day is National Son Day. The day when we celebrate our sons. National Son Day is one of the most celebrated special family days. This day is meant for the reverence and regard of the boy child and their guardians.

    ReplyDelete
  26. Data science course with placements in hyderabad
    we provide classroom training on IBM certified Data Science at hyderabad for the individuals who believe hand-held training. We teach as per the Indian Standard Time (IST) with In-depth practical Knowledge on each topic in classroom training, 80 – 90 Hrs of Real-time practical training classes. There are different slots available on weekends or weekdays according to your choices.

    ReplyDelete
  27. The Kingdom of Saudi Arabia was once called the Kingdom of Najd and the Hijaz. On September 23, 1332, under a royal decree of King Aziz Ibn Saud: The name changed to “HOUSE OF SAUD.” Also, it symbolizes the unity of the modern-day Kingdom of Saudi Arabia. Since 2005, Saudia has celebrated its Saudi Arabia National Day in a festive mood every September 23.

    ReplyDelete
  28. You might have been tired of looking for a streaming app that may help you download and stream content for free then why not download megabox hd 1.0.6 apk on your device because it shows content at different resolutions as you want. You can watch hundreds of movies and TV shows through this app.

    ReplyDelete