R - lapply, sapply, vapply, tapply and mapply

  • lapply
    • loop over a list and evaluate a function on each element
    •  > x <- list(a=c(1:3),b=c(4:6))  
       > lapply(x,sum)  
       $a  
       [1] 6  
       $b  
       [1] 15  
       > x <- 1:3  
       > lapply(x,runif)  # generate vector with number of elements with 1,2,3, uniform distribution on the interval from min(defalt:0) to max(default:1).
       [[1]]  
       [1] 0.8910183  
       [[2]]  
       [1] 0.6254153 0.2044462  
       [[3]]  
       [1] 0.05463454 0.37686384 0.45033247  
      
  • sapply
    • same as lapply but will try to simplify the result if possible.
    • That means, return vector if all elements in list with length 1, return matrix if all elements with the same length greater than 1, return list if none of these cases.
    •  > x <- list(a=c(1:5),b=c(4:5))  
       > lapply(x,sum)  
       $a  
       [1] 15  
       $b  
       [1] 9  
       > sapply(x,sum)  
        a b   
       15 9   
      
  • vapply
    • use vapply() to point out that what result format we expect, print error if not.
    •  > x <- data.frame(sex = c("male","female","male","female"),age = 26:29)  
       > x  
          sex age  
       1  male 26  
       2 female 27  
       3  male 28  
       4 female 29  
       > sapply(x,length)  
       sex age   
        4  4   
       > sapply(x,class)  
          sex    age   
        "factor" "integer"   
       > vapply(x, class, character(1))  # expect character(1) here as result format
          sex    age   
        "factor" "integer" 
      > table(x$sex) # check how many females or males in each group
      female   male 
           2      2   
      
  • tapply
    • apply a functions to each group of second variable
    • for example, we want to get the mean value of female and male groups
    •  > tapply(x$age, x$sex, mean)  
       female  male   
         28   27   
      
  • mapply 
    • applies a function in parallel over a set of arguments.
    •  > x <- list(rep(1,4),rep(2,3),rep(3,2),rep(4,1))  
       > x  
       [[1]]  
       [1] 1 1 1 1  
       [[2]]  
       [1] 2 2 2  
       [[3]]  
       [1] 3 3  
       [[4]]  
       [1] 4  
       > y <- mapply(rep, 1:4, 4:1)  # use mapply shortly achieve the same goal 
       > y  
       [[1]]  
       [1] 1 1 1 1  
       [[2]]  
       [1] 2 2 2  
       [[3]]  
       [1] 3 3  
       [[4]]  
       [1] 4  
      

No comments:

Post a Comment