Video není dostupné.
Omlouváme se.

How to Use lapply, sapply and mapply in R

Sdílet
Vložit
  • čas přidán 2. 02. 2020
  • This video shows how to use the lapply, sapply and mapply functions to execute a function on each element of a list or vector in R. The apply family of functions provide a convenient way to quickly invoke a function on each element of vectors without having to write custom for loops.
    Note that since data frames are lists where each column are separate elements of the list, the lapply and sapply will apply a function to each column when run on a data frame. This provides a quick way to create column-based summary statistics.
    Code used in this clip:
    Example of lapply
    data <- mtcars
    Function to apply
    mpg_category <- function(mpg){
    if(mpg > 30){
    return("High")
    } else if (mpg > 20){
    return("Medium")
    }
    return("Low")
    }
    Apply to each element
    lapply(X = data$mpg, FUN = mpg_category)
    Use sapply to simplify the result to a vector or matrix instead of a list
    sapply(X = data$mpg, FUN = mpg_category)
    You can pass additional arguments after FUN
    within_range <- function(mpg, low, high){
    if (mpg >= low & mpg <= high){
    return(TRUE)
    }
    return(FALSE)
    }
    index <- sapply(X = data$mpg, FUN = within_range, low = 15, high = 20)
    index
    data[index,]
    Use mapply to apply a function along multiple vectors at the same time
    Function to apply
    mpg_within_standard_range <- function(mpg, cyl){
    if (cyl == 4){
    return(within_range(mpg, low = 23, high = 31))
    } else if (cyl == 6) {
    return(within_range(mpg, low = 18, high = 23))
    }
    return(within_range(mpg, low = 13, high = 18))
    }
    index <- mapply(FUN = mpg_within_standard_range, mpg = data$mpg, cyl = data$cyl)
    index
    data[!index,]
    When used on a dataframe, lapply and sapply apply a function to each column
    sapply(data, FUN = median)
    * Note: CZcams does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! For R that means I may use = for assignment and the special Unicode large < and > symbols in place of the standard sized ones for dplyr pipes and comparisons. These special symbols should work as expected for R code on Windows, but may need to be replaced with standard greater than and less than symbols for other operating systems.

Komentáře • 48

  • @alfredstiassny4650
    @alfredstiassny4650 Před 4 lety +13

    Finally a clear explanation if these functions!

  • @smilynohn
    @smilynohn Před 3 lety +5

    I think this is the best explanation I have seen about these 3 functions. Practical as well. Thanks a lot!

    • @DataDaft
      @DataDaft  Před 3 lety +1

      Glad it was useful to you!

  • @designgoat2023
    @designgoat2023 Před 2 měsíci

    My man thank you SO much for being such an amazing teacher! 🙌

  • @joshroberts5661
    @joshroberts5661 Před 3 lety +8

    This was explained perfectly! I am officially subscribed. Thank you.

    • @DataDaft
      @DataDaft  Před 3 lety +2

      Thanks! Glad you found it helpful.

  • @yaweli2968
    @yaweli2968 Před 9 měsíci

    Clearly explained. Thanks

  • @alemarieceria3032
    @alemarieceria3032 Před rokem +1

    Awesome! This was super clear, concise and most importantly, helpful! Thank you for this!

  • @jjaa1962
    @jjaa1962 Před 4 lety +3

    Thank you so much for such a clear and simple presentation!

  • @mky1920
    @mky1920 Před 2 lety

    well explained. thank you

  • @konstantinosmaravegias4198

    Thank you. Plain and simple explanation.

  • @aronsantacruz4239
    @aronsantacruz4239 Před 3 lety

    Thanks! Greetings from Peru.

  • @jhilamsinha6217
    @jhilamsinha6217 Před 4 lety +2

    Great video !! very simple and effective..

  • @wilsonjp23
    @wilsonjp23 Před rokem

    Dude ty this exactly what I needed.

  • @rogerwilcoshirley2270
    @rogerwilcoshirley2270 Před 4 lety +1

    Nicely done ✅, very useful esp the mapply explanations.

  • @1961Lobo1961
    @1961Lobo1961 Před 2 lety

    Very well explained Thank you very much

  • @niceday2015
    @niceday2015 Před rokem

    Thank you very much

  • @TongPax
    @TongPax Před 3 lety

    Clear explanation. Nice.

  • @multitaskprueba1
    @multitaskprueba1 Před 2 lety

    Fantastic video! Thank you! You are a genius!

  • @larissacury7714
    @larissacury7714 Před 2 lety

    Thank you!

  • @pipertripp
    @pipertripp Před 2 lety

    very handy. Thanks for putting it together!

  • @octavius654321
    @octavius654321 Před 3 lety

    Thank you so much for the explanation

  • @harryporter2099
    @harryporter2099 Před 3 lety +2

    Thank you Sir!

  • @kartikeyasharma9908
    @kartikeyasharma9908 Před 4 lety +1

    Hey, thanks for uploading such an amazing video.

  • @ciantuohy4381
    @ciantuohy4381 Před 3 lety +1

    Thank you so much! This is great!

  • @xyzphd122
    @xyzphd122 Před rokem +1

    my lapply does not return it as 1. 2. 3. ... but with [[#]]... could you explain how you were able to set it to return in that format? for lapply? TY

  • @joelkosianza7694
    @joelkosianza7694 Před 3 lety

    Thanks for this amazing video

  • @javierlopezbahon
    @javierlopezbahon Před 3 lety +1

    Thank you!!!

  • @Ms-RAZ
    @Ms-RAZ Před 3 lety

    God bless you. Thank you!

  • @scottparrish7244
    @scottparrish7244 Před 3 lety

    Awesome. I really needed to see this video.

  • @chuckbecker4983
    @chuckbecker4983 Před 2 lety +1

    Okay, so I'm hung up on a trivial formatting thing, but when I run lapply my output includes all of the list indexes and looks nothing like the neatly serial numbered output at 1:50. My output:
    > lapply( X = data$mpg, FUN = mpg_func )
    [[1]]
    [1] "Medium"
    [[2]]
    [1] "Medium"
    [[3]]
    [1] "Medium"
    [[4]]
    [1] "Medium"
    [[5]]
    [1] "Low"
    Any help greatly appreciated!

    • @urjitaswar6661
      @urjitaswar6661 Před rokem +1

      Yeah, am facing the same issue.. also my sapply function is returning list() as an output (like an empty list) instead of the supposed output..

  • @meditationandrelaxation7460

    Amazing tutorial

  • @vincenzo4259
    @vincenzo4259 Před 2 lety

    Thanks

  • @totochandelier
    @totochandelier Před 4 lety +1

    thx !

  • @skyscraper5910
    @skyscraper5910 Před 3 lety

    Another maybe idea, if you showed what would happen if you tried to use lapply instead of mapply in the mapply example

  • @reubenjake8573
    @reubenjake8573 Před 4 lety

    What does the small x argument in lapply() do?

  • @skyscraper5910
    @skyscraper5910 Před 3 lety

    Would be helpful if the within_range function was still shown in the corner while showing the pithing_standard_range_funciton. otherwise, great stuff

  • @homataha5626
    @homataha5626 Před 3 lety

    what about the apply function? I am a little confused about when to use apply and when to use lappy?

  • @Orange-xw4lt
    @Orange-xw4lt Před 4 lety

    Hi, If I have a wave how can I take and separate the values ​​of the crests starting from a certain threshold?

    • @DataDaft
      @DataDaft  Před 4 lety +1

      Depends on how the data is stored, but if it is in a single vector you could use data[ data > threshold ] to get the values above a certain threshold.

    • @Orange-xw4lt
      @Orange-xw4lt Před 4 lety

      @@DataDaft the problem is to separate the values of the crests as individual intervals and calculate the average.

  • @jimaroddrigo7679
    @jimaroddrigo7679 Před 4 lety

    Dear sir, Could you please help me with my question?. I have to create data for an empty matrix and matrix size is big as ex. 450×450 . I used for loop but it takes longtime like more than 24 hrs since there are many functions are running inside the for loop for creating elements . So I heard we can use apply() function instead of for loop so could you please let me know that can we use apply() function to empty matrix . I have clear idea to use apply() non empty matrix but do not know how to fill a matrix using apply () ...

    • @DataDaft
      @DataDaft  Před 4 lety

      It is difficult to answer such a question without knowing the details of what you are trying to do and how you are creating/populating data. You could start with a matrix of the desired size filled with NA and then use apply to generate columns of values by passing in a custom function that creates vectors that are the same length as your columns. Something like:
      empty

  • @khaibaromari8178
    @khaibaromari8178 Před 3 lety

    I was wondering which IDE is he using? is it jupyter for R lol

    • @DataDaft
      @DataDaft  Před 3 lety

      I'm using the Kaggle Notebook environment, which is a basically an R Jupyter notebook. You can sign up for an account on Kaggle and make a new R notebook if you want to replicate this environment. It isn't really the best in terms of an IDE, it is just convenient because it allows for public sharing of code and anyone wants to can use it and should get the same results as what is shown in the videos.

  • @shalinisarathy3002
    @shalinisarathy3002 Před 3 lety

    very nice explanation. Thank you.