Learn how to use lapply in R

0

The lapply function in R applies a function to elements in a list or a vector and returns the results in a list.

lapply(list, function, …)

The lapply function is best for working with data frames. In R the data frame is considered a list and the variables in the data frame are the elements of the list. Unlike the apply function, there is no margin argument when applying the lapply function to each component of the list.

The lapply function in R will SPLIT up data into smaller pieces, APPLY a function to each piece, then COMBINE the results.

Swirl has great examples built in for learning what lapply can do. Within the package there is a flags dataset. If you run the dataset you will see the following dataframe.


> head(flags)
            name landmass zone area population language religion bars stripes colours red green
1    Afghanistan        5    1  648         16       10        2    0       3       5   1     1
2        Albania        3    1   29          3        6        6    0       0       3   1     0
3        Algeria        4    1 2388         20        8        2    2       0       3   1     1
4 American-Samoa        6    3    0          0        1        1    0       0       5   1     0
5        Andorra        3    1    0          0        6        0    3       0       3   1     0
6         Angola        4    2 1247          7       10        5    0       2       3   1     0
  blue gold white black orange mainhue circles crosses saltires quarters sunstars crescent triangle
1    0    1     1     1      0   green       0       0        0        0        1        0        0
2    0    1     0     1      0     red       0       0        0        0        1        0        0
3    0    0     1     0      0   green       0       0        0        0        1        1        0
4    1    1     1     0      1    blue       0       0        0        0        0        0        1
5    1    1     0     0      0    gold       0       0        0        0        0        0        0
6    0    1     0     1      0     red       0       0        0        0        1        0        0
  icon animate text topleft botright
1    1       0    0   black    green
2    0       1    0     red      red
3    0       0    0   green    white
4    1       1    0    blue      red
5    0       0    0    blue      red
6    1       0    0     red    black

The best example is to run lapply to find out the class:

>cls_list <- lapply(flags, class) 

This will apply the class function to each column of the flags dataset and store the result in a variable called cls_list.

Type the following:

> cls_list

This will return the class of each element in the list:


$name
[1] "factor"

$landmass
[1] "integer"

$zone
[1] "integer"

$area
[1] "integer"

$population
[1] "integer"

$language
[1] "integer"

$religion
[1] "integer"

$bars
[1] "integer"

$stripes
[1] "integer"

$colours
[1] "integer"

$red
[1] "integer"

$green
[1] "integer"

$blue
[1] "integer"

$gold
[1] "integer"

$white
[1] "integer"

$black
[1] "integer"

$orange
[1] "integer"

$mainhue
[1] "factor"

$circles
[1] "integer"

$crosses
[1] "integer"

$saltires
[1] "integer"

$quarters
[1] "integer"

$sunstars
[1] "integer"

$crescent
[1] "integer"

$triangle
[1] "integer"

$icon
[1] "integer"

$animate
[1] "integer"

$text
[1] "integer"

$topleft
[1] "factor"

$botright
[1] "factor"

There you have it, lapply returned a list of classes for the flags dataset from with the dataframe.

LEAVE A REPLY

Please enter your comment!
Please enter your name here