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:


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

        [,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() 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.
    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.
       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 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)
[1] 5.5
[1] 15.5
class(lapply(l, mean))
[1] "list
  # the sum of the values in each element 
lapply(l, sum)
[1] 55

[1] 155

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() 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.
 num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
[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.

       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 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.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 ...

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() 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'))
 [1]  1  2  3  4  5  6  7  8  9 10

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

[1] "d" "a" "t" "a"

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

[1] 15.5


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

[1] 15.5

[1] "d" "a" "t" "a"

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(, 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"


  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

  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

  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

  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

  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

  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

  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 

  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

  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

  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.

  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

  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

  13. Beautiful illustration of those concepts..

  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

  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

  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

  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

  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

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

  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

  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 rental singapore

  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.

  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:

  24. 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.

  25. 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.

  26. Leopard courier service is one of the most reliable and viable courier services in Pakistan. This is to say that Leopard courier enables you to send all the essentials within your cities or wherever you want. Above all, It is the fastest-growing efulfillment service in Pakistan. Consequently, If you want to be a part of this successful courier company and want to buy Leopard Courier Franchise, we will get you through.

  27. Are you looking for a new bong or water pipe but don't have much to spend? At Grascity, we have an extensive range of cheap bongs in our assortment that don't skimp on quality. Bongs can be expensive smoking devices. That's why we made a section of notch cheap bongs that don't break the bank. We offer cheap glass bongs, mini bongs, acrylic bongs, silicone bongs, and ceramic bongs in our selection of affordable bongs. Get yourself a cheap bong now. cheap bongs

  28. Here are 7 tips for limiting or quitting porn use:
    • Try Meditation
    • Engage in Physical Activity
    • Change your Surroundings
    • Install a Porn Blocker
    • Try Acceptance and Commitment Therapy
    • Accept Yourself
    • Prioritize Your Values
    For more information about how to stop watching porn, signup today at

  29. Hello
    Its really an amazing blog. Music, art concerned with combining vocal or instrumental sounds for beauty of form or emotional expression. You wanna try best acoustic electric guitars for the relaxation of mind and body.
    Thank you

  30. As you can see, there are different things that determine the sound level of a paintball gun. While people prefer getting a quiet paintball gun to be considerate of their neighbors, many actually look for noisy guns on purpose. The reason behind this is that they only use their loud paintball guns on designated paintball fields and so the production of sound does not affect them. Moreover, a loud game actually resembles a real war scene more, and so people enjoy it when their paintball guns produce a lot of noise.

  31. Leopard Cash on Delivery:

    Leopard Courier Charges For packages up to 500 grams, Rs. 165 within city and Rs. 195 for 1kg or Rs. 150 for each additional kg

  32. Really very grateful for these tips you visit on site best paper writing websites for shared with us in this article. as resume cover letters and CVs is the primary step towards the Job. So it must be effective and attractive enough. By following these steps I designed my cover letter and got a call from many organizations for the interview.

  33. Open your COD account now by emailing us at, or Give us a call on 111-300-786 to book an appointment with our sales representative. Leopards Courier Service (Pvt.) Ltd. Please follow the below-mentioned link and fill out the form.

  34. Machine embroidery is a a laugh stitching method that permits you to customize readymade items and custom sewing tasks. It’s a simple embellishment to examine when you recognize the basics, due to the fact the gadget does most of the be just right for you!

  35. If you are deliberating selling your property, repairing and portray the garage floor is one of these affordable matters you may do to improve the appearance of your own home without spending quite a few money and time on thepaintly.
    It increase the Value of Your Home

  36. Are you looking for one of the best Offset smoker under $1500? I am here to give you a complete guide about those offset smokers that will enhance your experience of barbeque, along with retaining the juicy and smokiness of your meal.
    best offset smoker under 1500

  37. Wearing relaxed shoes may additionally yield copious fitness blessings to you. Wearing beside the point shoes can cause excessive fitness troubles without delay on the for together with calluses, bunions, plantar fasciitis and spurs, and so forth. When those illnesses aggravate, it could have an effect on your typical health.

  38. Tank size is an important factor to consider while choosing the right compressor. The larger the tank; the more powerful the compressor is. So, if you need more power to perform your work; then, go for a tank that is more than 6 gallons. However, for most home-based tasks; a tank of 4-6 gallons is usually fine. Are you searching for the Top 3 best compressors so that you can buy one for yourself? Then, go to my site where are our top picks for you.

  39. Losing in a game is also a part of life. A team of this game lose the match and work hard for next time for winning the match. Many of the reason are behind for losing the match. It is compulsory for every team boy to work hard and start practice for next watch.

  40. Projector headlights are high performing headlights. These headlights are different types of lamps. They deliver enormous amounts of light and have an even distribution. In the past the projector headlights could be installed in only luxury vehicles but nowadays they can be installed in a wide range of vehicles.

  41. Convert pressure from bar to PSI and vice versa? Heard of Pascal? We give you the complete explanation, tell you how to make the conversion and illustrate it with concrete examples.
    converting pressure from psi bar to bar psi

  42. Even though this year is a bit different, living in a warm climate during the winter has its perks. There tend to be more people here and the "snowbirds" flee from the snow which means that there are more people available who might want to make their winter vacation spot a permeate residence.
    homes for sale by owner tampa fl

  43. Hey, this blog is helpful for us. thanks for publish it here. Sonalika Tractor

  44. Vocabulary is only part of overseas language however the most crucial one. If you want to examine foreign language, begin with the phrases, first. As nicely as kids do. When you know a few phrases you can try to speak. It will no longer be best in the beginning but don’t fear, humans will understand you.

  45. Social media is easily available and it’s additionally the assembly factor of these days internet savvy target market. Major portion of more youthful generation, teenagers and center elderly people, are predominant percentage of the whole social media user population.

  46. Lola Marie was the see here women who always thinking about the poor people and she is the role model of the people. She spends the good life and people still appreciate her so much and she always loves their family so much. It is not a good news.


  47. This blog is perfect and educative thanks for share this experience with us.

    Trakstar tractor

  48. Vacant land and residential plenty, plus the homes, outbuildings, decks, timber sewers and furniture inside the obstacles of the assets are examples of real property. Furniture, cars, artwork, rings and boats are examples of private property rather than real estate.

  49. Board games are tabletop games that typically use pieces. These pieces are moved or placed on a pre-marked board and often include elements of table, card, role-playing, and miniatures games as well.

  50. Epoxy flooring is becoming a popular alternative to traditional flooring options such as tiles, carpet, vinyl and laminate flooring. Epoxy flooring phoenix provide complete functionality for residential, commercial and industrial sites throughout Phoenix, with no joints, edges or seams to contend with.

  51. Are you looking for the best epoxy flooring service in Phoenix? Marvelous Epoxy Flooring is the right company to help with all your epoxy flooring related needs. Whether you need epoxy flooring or garage flooring anthem services in Gilbert, Scottsdale or Mesa, our team of experts can handle your floors neatly, quickly, and professionally.


  52. Hey, The article which you share here is superb and admirable
    Swaraj Tractor

  53. Nice article and keep to update the great article like this
    Liveaboards in Indonesia