purrr allows us to simplify iteration, either with vectors or lists without having to deal with for loops. Iteration allows us to do same thing again and again with different inputs. This means we don’t have to write repetitive lines of code for different inputs.Iteration saves time, reduces line of code and reduces typos.For loops are powerful but often the place where typos which are tough to identify pop-up and this prevents you from moving on in your workflow.
# Initialize list
all_files <- list()
# For loop to read files into a list
for(i in seq_along(files)){
all_files[[i]] <- read_csv(file = files[[i]])
}
# Output size of list object
length(all_files)purrr makes iteration easy. It wraps a for loop into a single function map(object, function)(object can be list or vector) which reduces the number of lines of code we need and allows us to focus on important pieces of what we are trying to accomplish. Instead of worrying about indices, brackets or curly braces we can just put in the pieces and get what we need.
# Use map to iterate
all_files_purrr <- map(files, read_csv)
# Output size of list object
length(all_files_purrr)
## Bird Counts Example using For loop
bird_sum <- list()
for(i in seq_along(bird_counts)){
bird_sum[[i]] <- sum(bird_counts[[i]])
}
## Using Map
bird_sum <- map(bird_counts, sum)# Check the class type of the first element
class(list_of_df[[1]])
# Change each element from a character to a number
for(i in seq_along(list_of_df)){
list_of_df[[i]] <- as.numeric(list_of_df[[i]])
}
# Change each character element to a number using map
list_of_df <- map(list_of_df, as.numeric)
# Check the class type of the first element again
class(list_of_df[[1]])
# Print out the list
list_of_dfLists require different grammar compared to dataframes or vectors. Lists can store different data types, they can hold a wide variety of things. The contents of a list don’t have to be the same data type, and as long as you know how it’s organized, you can grab out what you need by sub-setting.
Both named and unnamed lists can be subset using double square brackets [[ ]] OR listname[[ index ]]
If a list is named, you can also use $ for subsetting. The syntax list$elementname pulls out the named element from the list. Like any other kind of object in R, you can use the str() to determine the structure of the list.
lo <- list()
lo[["data"]] <- data.frame(bird = c("robin", "sparrow", "jay"), weight = c(76, 14, 100), wing_length = c(100, 35, 130))
lo[["model"]] <- lm(weight ~ wing_length,data = lo[["data"]])
lo[["plot"]] <- ggplot(data = lo[["model"]], aes(x = weight, y = wing_length)) + geom_point()
# Create a dataframe to place the results in
df_rows <- data.frame(names = names(survey_data), rows = NA)
# Loop over survey_data to determine how many rows are in each element
for(i in 1:length(survey_data)){
df_rows[i,'rows'] <- nrow(survey_data[[i]])
}
map(survey_data, ~nrow(.x))You can also subset within list elements using bracket notation like this: ListName$ElementName[VectorNumber]. If a list element is a dataframe, you can pull out a column like this: ListName$ElementName$ColumnName or ListName[[1]][,1].
# Load repurrrsive package, to get access to the wesanderson dataset
library(repurrrsive)
# Load wesanderson dataset
data(wesanderson)
data(sw_films)
# Get structure of first element in wesanderson
str(wesanderson[1])## List of 1
## $ GrandBudapest: chr [1:4] "#F1BB7B" "#FD6467" "#5B1A18" "#D67236"
## chr [1:4] "#F1BB7B" "#FD6467" "#5B1A18" "#D67236"
## [1] "#5B1A18"
## [1] "#D67236"
## $title
## [1] "A New Hope"
##
## $episode_id
## [1] 4
##
## $opening_crawl
## [1] "It is a period of civil war.\r\nRebel spaceships, striking\r\nfrom a hidden base, have won\r\ntheir first victory against\r\nthe evil Galactic Empire.\r\n\r\nDuring the battle, Rebel\r\nspies managed to steal secret\r\nplans to the Empire's\r\nultimate weapon, the DEATH\r\nSTAR, an armored space\r\nstation with enough power\r\nto destroy an entire planet.\r\n\r\nPursued by the Empire's\r\nsinister agents, Princess\r\nLeia races home aboard her\r\nstarship, custodian of the\r\nstolen plans that can save her\r\npeople and restore\r\nfreedom to the galaxy...."
##
## $director
## [1] "George Lucas"
##
## $producer
## [1] "Gary Kurtz, Rick McCallum"
##
## $release_date
## [1] "1977-05-25"
##
## $characters
## [1] "http://swapi.co/api/people/1/" "http://swapi.co/api/people/2/"
## [3] "http://swapi.co/api/people/3/" "http://swapi.co/api/people/4/"
## [5] "http://swapi.co/api/people/5/" "http://swapi.co/api/people/6/"
## [7] "http://swapi.co/api/people/7/" "http://swapi.co/api/people/8/"
## [9] "http://swapi.co/api/people/9/" "http://swapi.co/api/people/10/"
## [11] "http://swapi.co/api/people/12/" "http://swapi.co/api/people/13/"
## [13] "http://swapi.co/api/people/14/" "http://swapi.co/api/people/15/"
## [15] "http://swapi.co/api/people/16/" "http://swapi.co/api/people/18/"
## [17] "http://swapi.co/api/people/19/" "http://swapi.co/api/people/81/"
##
## $planets
## [1] "http://swapi.co/api/planets/2/" "http://swapi.co/api/planets/3/"
## [3] "http://swapi.co/api/planets/1/"
##
## $starships
## [1] "http://swapi.co/api/starships/2/" "http://swapi.co/api/starships/3/"
## [3] "http://swapi.co/api/starships/5/" "http://swapi.co/api/starships/9/"
## [5] "http://swapi.co/api/starships/10/" "http://swapi.co/api/starships/11/"
## [7] "http://swapi.co/api/starships/12/" "http://swapi.co/api/starships/13/"
##
## $vehicles
## [1] "http://swapi.co/api/vehicles/4/" "http://swapi.co/api/vehicles/6/"
## [3] "http://swapi.co/api/vehicles/7/" "http://swapi.co/api/vehicles/8/"
##
## $species
## [1] "http://swapi.co/api/species/5/" "http://swapi.co/api/species/3/"
## [3] "http://swapi.co/api/species/2/" "http://swapi.co/api/species/1/"
## [5] "http://swapi.co/api/species/4/"
##
## $created
## [1] "2014-12-10T14:23:31.880000Z"
##
## $edited
## [1] "2015-04-11T09:46:52.774897Z"
##
## $url
## [1] "http://swapi.co/api/films/1/"
## [1] "A New Hope"
In the previous example we saw that map outputs a list. Sometimes this helps but if we look at the kinds of problem we solve everyday we may want a different kind of output like a vector.
map_dbl() - returns a vector of numbers
map_lgl() - returns a logical vector
map_chr() - returns a character vector
map_int() - returns a integer vector
# Determine row number
map_dbl(survey_data, ~nrow(.x))
# Determine if elements have 14 rows
map_lgl(survey_data, ~nrow(.x)==14)
# Map over species_names list
map_chr(species_names, ~.x)
# Create a dataframe called survey_rows
survey_rows <- data.frame(names = names(survey_data),
rows = NA)
# Map over survey_data to determine row number in each element
survey_rows$rows <- map_dbl(survey_data, ~nrow(.x))
# Print out the survey_rows dataframe
survey_rowsmap(list, ~function(.x)) gives the same result as map(list, function). To specify how the list is used in the function, use the argument .x to denote where the list element goes inside the function. When you want to use .x to show where the element goes in the function, you need to put a ~ in front of the function in the second argument of map()
## $GrandBudapest
## [1] 4
##
## $Moonrise1
## [1] 4
##
## $Royal1
## [1] 4
##
## $Moonrise2
## [1] 4
##
## $Cavalcanti
## [1] 5
##
## $Royal2
## [1] 5
##
## $GrandBudapest2
## [1] 4
##
## $Moonrise3
## [1] 5
##
## $Chevalier
## [1] 4
##
## $Zissou
## [1] 5
##
## $FantasticFox
## [1] 5
##
## $Darjeeling
## [1] 5
##
## $Rushmore
## [1] 5
##
## $BottleRocket
## [1] 7
##
## $Darjeeling2
## [1] 5
## $GrandBudapest
## [1] 4
##
## $Moonrise1
## [1] 4
##
## $Royal1
## [1] 4
##
## $Moonrise2
## [1] 4
##
## $Cavalcanti
## [1] 5
##
## $Royal2
## [1] 5
##
## $GrandBudapest2
## [1] 4
##
## $Moonrise3
## [1] 5
##
## $Chevalier
## [1] 4
##
## $Zissou
## [1] 5
##
## $FantasticFox
## [1] 5
##
## $Darjeeling
## [1] 5
##
## $Rushmore
## [1] 5
##
## $BottleRocket
## [1] 7
##
## $Darjeeling2
## [1] 5
# Create a numcolors column and fill with length of each wesanderson element
data.frame(numcolors = map_dbl(wesanderson, ~length(.x)))## numcolors
## GrandBudapest 4
## Moonrise1 4
## Royal1 4
## Moonrise2 4
## Cavalcanti 5
## Royal2 5
## GrandBudapest2 4
## Moonrise3 5
## Chevalier 4
## Zissou 5
## FantasticFox 5
## Darjeeling 5
## Rushmore 5
## BottleRocket 7
## Darjeeling2 5
Since purrr is a part of set of packages called as tidyverse it works well with pipes %>%. Pipes take the output from the function in front of it and makes it the the input for the next function. Pipes are powerful because they prevent us from needing intermediate objects. If a list doesn’t have name we can set them which is helpful for indexing, sub-setting and generally working with a list. Especially one that might grow or change over time where numeric indexing ultimately won’t be useful.
names(survey_data)
survey_data %>% names()
# Pipes within Map
map(waterfowl_data, ~.x %>% sum() %>% log())## List of 7
## $ :List of 14
## ..$ title : chr "A New Hope"
## ..$ episode_id : int 4
## ..$ opening_crawl: chr "It is a period of civil war.\r\nRebel spaceships, striking\r\nfrom a hidden base, have won\r\ntheir first victo"| __truncated__
## ..$ director : chr "George Lucas"
## ..$ producer : chr "Gary Kurtz, Rick McCallum"
## ..$ release_date : chr "1977-05-25"
## ..$ characters : chr [1:18] "http://swapi.co/api/people/1/" "http://swapi.co/api/people/2/" "http://swapi.co/api/people/3/" "http://swapi.co/api/people/4/" ...
## ..$ planets : chr [1:3] "http://swapi.co/api/planets/2/" "http://swapi.co/api/planets/3/" "http://swapi.co/api/planets/1/"
## ..$ starships : chr [1:8] "http://swapi.co/api/starships/2/" "http://swapi.co/api/starships/3/" "http://swapi.co/api/starships/5/" "http://swapi.co/api/starships/9/" ...
## ..$ vehicles : chr [1:4] "http://swapi.co/api/vehicles/4/" "http://swapi.co/api/vehicles/6/" "http://swapi.co/api/vehicles/7/" "http://swapi.co/api/vehicles/8/"
## ..$ species : chr [1:5] "http://swapi.co/api/species/5/" "http://swapi.co/api/species/3/" "http://swapi.co/api/species/2/" "http://swapi.co/api/species/1/" ...
## ..$ created : chr "2014-12-10T14:23:31.880000Z"
## ..$ edited : chr "2015-04-11T09:46:52.774897Z"
## ..$ url : chr "http://swapi.co/api/films/1/"
## $ :List of 14
## ..$ title : chr "Attack of the Clones"
## ..$ episode_id : int 2
## ..$ opening_crawl: chr "There is unrest in the Galactic\r\nSenate. Several thousand solar\r\nsystems have declared their\r\nintentions "| __truncated__
## ..$ director : chr "George Lucas"
## ..$ producer : chr "Rick McCallum"
## ..$ release_date : chr "2002-05-16"
## ..$ characters : chr [1:40] "http://swapi.co/api/people/2/" "http://swapi.co/api/people/3/" "http://swapi.co/api/people/6/" "http://swapi.co/api/people/7/" ...
## ..$ planets : chr [1:5] "http://swapi.co/api/planets/8/" "http://swapi.co/api/planets/9/" "http://swapi.co/api/planets/10/" "http://swapi.co/api/planets/11/" ...
## ..$ starships : chr [1:9] "http://swapi.co/api/starships/21/" "http://swapi.co/api/starships/39/" "http://swapi.co/api/starships/43/" "http://swapi.co/api/starships/47/" ...
## ..$ vehicles : chr [1:11] "http://swapi.co/api/vehicles/4/" "http://swapi.co/api/vehicles/44/" "http://swapi.co/api/vehicles/45/" "http://swapi.co/api/vehicles/46/" ...
## ..$ species : chr [1:14] "http://swapi.co/api/species/32/" "http://swapi.co/api/species/33/" "http://swapi.co/api/species/2/" "http://swapi.co/api/species/35/" ...
## ..$ created : chr "2014-12-20T10:57:57.886000Z"
## ..$ edited : chr "2015-04-11T09:45:01.623982Z"
## ..$ url : chr "http://swapi.co/api/films/5/"
## $ :List of 14
## ..$ title : chr "The Phantom Menace"
## ..$ episode_id : int 1
## ..$ opening_crawl: chr "Turmoil has engulfed the\r\nGalactic Republic. The taxation\r\nof trade routes to outlying star\r\nsystems is i"| __truncated__
## ..$ director : chr "George Lucas"
## ..$ producer : chr "Rick McCallum"
## ..$ release_date : chr "1999-05-19"
## ..$ characters : chr [1:34] "http://swapi.co/api/people/2/" "http://swapi.co/api/people/3/" "http://swapi.co/api/people/10/" "http://swapi.co/api/people/11/" ...
## ..$ planets : chr [1:3] "http://swapi.co/api/planets/8/" "http://swapi.co/api/planets/9/" "http://swapi.co/api/planets/1/"
## ..$ starships : chr [1:5] "http://swapi.co/api/starships/40/" "http://swapi.co/api/starships/41/" "http://swapi.co/api/starships/31/" "http://swapi.co/api/starships/32/" ...
## ..$ vehicles : chr [1:7] "http://swapi.co/api/vehicles/33/" "http://swapi.co/api/vehicles/34/" "http://swapi.co/api/vehicles/35/" "http://swapi.co/api/vehicles/36/" ...
## ..$ species : chr [1:20] "http://swapi.co/api/species/1/" "http://swapi.co/api/species/2/" "http://swapi.co/api/species/6/" "http://swapi.co/api/species/11/" ...
## ..$ created : chr "2014-12-19T16:52:55.740000Z"
## ..$ edited : chr "2015-04-11T09:45:18.689301Z"
## ..$ url : chr "http://swapi.co/api/films/4/"
## $ :List of 14
## ..$ title : chr "Revenge of the Sith"
## ..$ episode_id : int 3
## ..$ opening_crawl: chr "War! The Republic is crumbling\r\nunder attacks by the ruthless\r\nSith Lord, Count Dooku.\r\nThere are heroes "| __truncated__
## ..$ director : chr "George Lucas"
## ..$ producer : chr "Rick McCallum"
## ..$ release_date : chr "2005-05-19"
## ..$ characters : chr [1:34] "http://swapi.co/api/people/1/" "http://swapi.co/api/people/2/" "http://swapi.co/api/people/3/" "http://swapi.co/api/people/4/" ...
## ..$ planets : chr [1:13] "http://swapi.co/api/planets/2/" "http://swapi.co/api/planets/5/" "http://swapi.co/api/planets/8/" "http://swapi.co/api/planets/9/" ...
## ..$ starships : chr [1:12] "http://swapi.co/api/starships/48/" "http://swapi.co/api/starships/59/" "http://swapi.co/api/starships/61/" "http://swapi.co/api/starships/32/" ...
## ..$ vehicles : chr [1:13] "http://swapi.co/api/vehicles/33/" "http://swapi.co/api/vehicles/50/" "http://swapi.co/api/vehicles/60/" "http://swapi.co/api/vehicles/62/" ...
## ..$ species : chr [1:20] "http://swapi.co/api/species/19/" "http://swapi.co/api/species/33/" "http://swapi.co/api/species/2/" "http://swapi.co/api/species/3/" ...
## ..$ created : chr "2014-12-20T18:49:38.403000Z"
## ..$ edited : chr "2015-04-11T09:45:44.862122Z"
## ..$ url : chr "http://swapi.co/api/films/6/"
## $ :List of 14
## ..$ title : chr "Return of the Jedi"
## ..$ episode_id : int 6
## ..$ opening_crawl: chr "Luke Skywalker has returned to\r\nhis home planet of Tatooine in\r\nan attempt to rescue his\r\nfriend Han Solo"| __truncated__
## ..$ director : chr "Richard Marquand"
## ..$ producer : chr "Howard G. Kazanjian, George Lucas, Rick McCallum"
## ..$ release_date : chr "1983-05-25"
## ..$ characters : chr [1:20] "http://swapi.co/api/people/1/" "http://swapi.co/api/people/2/" "http://swapi.co/api/people/3/" "http://swapi.co/api/people/4/" ...
## ..$ planets : chr [1:5] "http://swapi.co/api/planets/5/" "http://swapi.co/api/planets/7/" "http://swapi.co/api/planets/8/" "http://swapi.co/api/planets/9/" ...
## ..$ starships : chr [1:12] "http://swapi.co/api/starships/10/" "http://swapi.co/api/starships/11/" "http://swapi.co/api/starships/12/" "http://swapi.co/api/starships/15/" ...
## ..$ vehicles : chr [1:8] "http://swapi.co/api/vehicles/8/" "http://swapi.co/api/vehicles/16/" "http://swapi.co/api/vehicles/18/" "http://swapi.co/api/vehicles/19/" ...
## ..$ species : chr [1:9] "http://swapi.co/api/species/5/" "http://swapi.co/api/species/6/" "http://swapi.co/api/species/8/" "http://swapi.co/api/species/9/" ...
## ..$ created : chr "2014-12-18T10:39:33.255000Z"
## ..$ edited : chr "2015-04-11T09:46:05.220365Z"
## ..$ url : chr "http://swapi.co/api/films/3/"
## $ :List of 14
## ..$ title : chr "The Empire Strikes Back"
## ..$ episode_id : int 5
## ..$ opening_crawl: chr "It is a dark time for the\r\nRebellion. Although the Death\r\nStar has been destroyed,\r\nImperial troops have "| __truncated__
## ..$ director : chr "Irvin Kershner"
## ..$ producer : chr "Gary Kutz, Rick McCallum"
## ..$ release_date : chr "1980-05-17"
## ..$ characters : chr [1:16] "http://swapi.co/api/people/1/" "http://swapi.co/api/people/2/" "http://swapi.co/api/people/3/" "http://swapi.co/api/people/4/" ...
## ..$ planets : chr [1:4] "http://swapi.co/api/planets/4/" "http://swapi.co/api/planets/5/" "http://swapi.co/api/planets/6/" "http://swapi.co/api/planets/27/"
## ..$ starships : chr [1:9] "http://swapi.co/api/starships/10/" "http://swapi.co/api/starships/11/" "http://swapi.co/api/starships/12/" "http://swapi.co/api/starships/15/" ...
## ..$ vehicles : chr [1:6] "http://swapi.co/api/vehicles/8/" "http://swapi.co/api/vehicles/14/" "http://swapi.co/api/vehicles/16/" "http://swapi.co/api/vehicles/18/" ...
## ..$ species : chr [1:5] "http://swapi.co/api/species/6/" "http://swapi.co/api/species/7/" "http://swapi.co/api/species/3/" "http://swapi.co/api/species/2/" ...
## ..$ created : chr "2014-12-12T11:26:24.656000Z"
## ..$ edited : chr "2015-04-11T09:46:31.433607Z"
## ..$ url : chr "http://swapi.co/api/films/2/"
## $ :List of 13
## ..$ title : chr "The Force Awakens"
## ..$ episode_id : int 7
## ..$ opening_crawl: chr "Luke Skywalker has vanished.\r\nIn his absence, the sinister\r\nFIRST ORDER has risen from\r\nthe ashes of the "| __truncated__
## ..$ director : chr "J. J. Abrams"
## ..$ producer : chr "Kathleen Kennedy, J. J. Abrams, Bryan Burk"
## ..$ release_date : chr "2015-12-11"
## ..$ characters : chr [1:11] "http://swapi.co/api/people/1/" "http://swapi.co/api/people/3/" "http://swapi.co/api/people/5/" "http://swapi.co/api/people/13/" ...
## ..$ planets : chr "http://swapi.co/api/planets/61/"
## ..$ starships : chr [1:2] "http://swapi.co/api/starships/77/" "http://swapi.co/api/starships/10/"
## ..$ species : chr [1:3] "http://swapi.co/api/species/3/" "http://swapi.co/api/species/2/" "http://swapi.co/api/species/1/"
## ..$ created : chr "2015-04-17T06:51:30.504780Z"
## ..$ edited : chr "2015-12-17T14:31:47.617768Z"
## ..$ url : chr "http://swapi.co/api/films/7/"
## [1] "A New Hope" "Attack of the Clones"
## [3] "The Phantom Menace" "Revenge of the Sith"
## [5] "Return of the Jedi" "The Empire Strikes Back"
## [7] "The Force Awakens"
# Create a list of values from 1 through 10
numlist <- list(1:10)
# Iterate over the numlist
map(numlist, ~.x %>% sqrt() %>% sin())## [[1]]
## [1] 0.84147098 0.98776595 0.98702664 0.90929743 0.78674913 0.63815764
## [7] 0.47577184 0.30807174 0.14112001 -0.02068353
Different tasks that can be done with map like simulating data, running linear models and outputting different classes of information.
# list_of_df contains list of means that we want to use generate/simulate datasets
list_of_df <- map(list_of_means, ~data.frame(a = rnorm(mean = .x, n = 200, sd = (5/2))))
# education_data is a list of two datasets, we applied the linear model
# formula on both datasets one-by-one and then summarize the model
models <- education_data %>% map(~ lm(income ~ education_level, data=.x)) %>% map(summary)
map_chr(livingthings, ~.x[["species"]])
map_lgl(livingthings, ~.x[["species"]]=="Purple Flowers")
map_dbl(bird_measurements, ~.x[["weight"]])
map_int(bird_measurements, ~.x[["wing length"]])Till now we have seen map returning vectors only which is one-dimensional data. Oftentimes we want a dataframe which is two-dimensional. Dataframes have great utility and are a required input for many R functions.
bird_measurements %>%
map_df(~ data_frame(weight=.x[["weight"]], wing_length = .x[["wing length"]]))# List of sites north, east, and west
sites <- list("north", "east", "west")
# Create a list of dataframes, each with a years, a, and b column
list_of_df <- map(sites,
~data.frame(sites = .x,
a = rnorm(mean = 5, n = 200, sd = (5/2)),
b = rnorm(mean = 200, n = 200, sd = 15)))
list_of_df## [[1]]
## sites a b
## 1 north 3.97616671 202.0769
## 2 north 4.05091960 218.9514
## 3 north 8.03937680 199.5588
## 4 north 5.27338680 214.7802
## 5 north 6.20274797 194.2441
## 6 north 9.01864184 218.4840
## 7 north 8.29413255 205.7070
## 8 north 6.68484718 217.0811
## 9 north 4.13365936 165.5650
## 10 north 6.12254945 194.7986
## 11 north 6.39987168 204.2222
## 12 north 4.37116540 181.1408
## 13 north 5.17217057 172.8057
## 14 north 2.86700101 238.0278
## 15 north 2.33341864 181.8626
## 16 north 8.40593730 210.8515
## 17 north 5.52441136 233.7396
## 18 north 3.06844729 209.8683
## 19 north 8.63489095 165.5810
## 20 north 6.02508649 187.5543
## 21 north 4.99572979 204.1705
## 22 north 7.14289354 188.3075
## 23 north 7.21304713 184.8220
## 24 north 3.75509256 203.6496
## 25 north 8.47693358 224.9416
## 26 north 2.00901426 218.2568
## 27 north 2.92778446 196.6678
## 28 north 3.92696317 173.6005
## 29 north 4.62914241 205.5437
## 30 north 2.89419862 242.2977
## 31 north 4.06895404 203.3184
## 32 north 5.22578121 191.7277
## 33 north 2.20694528 202.2047
## 34 north 2.12989604 177.4215
## 35 north 3.96595248 200.1323
## 36 north 4.80076955 220.9905
## 37 north -0.15010132 217.6054
## 38 north 1.35783383 214.9211
## 39 north 1.70559310 203.5081
## 40 north 1.72072647 179.4660
## 41 north 8.23383931 193.4232
## 42 north 6.92170924 215.3818
## 43 north 5.01279608 231.0283
## 44 north 5.81438478 222.2750
## 45 north 3.44673833 187.1036
## 46 north 3.81472047 178.4033
## 47 north 3.75737752 186.4296
## 48 north 1.57792727 199.8283
## 49 north 7.82953623 244.5454
## 50 north 4.26413633 190.8305
## 51 north 2.90456168 188.7087
## 52 north 5.45324176 178.6629
## 53 north 11.71279215 177.3492
## 54 north 6.62352805 196.6911
## 55 north 8.26830553 206.5904
## 56 north 1.64490110 231.1009
## 57 north 8.71339088 233.5274
## 58 north 4.31849064 192.1571
## 59 north 6.92315157 178.5459
## 60 north 6.80195434 164.1419
## 61 north 7.37155125 225.9697
## 62 north 4.29759624 199.8388
## 63 north 6.81568920 192.2653
## 64 north 5.35261913 192.3095
## 65 north 4.75058926 174.9958
## 66 north 2.22216188 181.9853
## 67 north 2.21504341 217.4847
## 68 north 4.97338253 189.9095
## 69 north 4.94300282 204.5743
## 70 north 1.05977236 193.2176
## 71 north 2.52305176 196.3069
## 72 north 8.79584702 184.1963
## 73 north 8.48718024 200.1890
## 74 north 5.54188064 205.4675
## 75 north 5.56780201 231.6640
## 76 north 0.77800490 206.5989
## 77 north 7.25491720 205.6044
## 78 north -0.35660907 203.5625
## 79 north 7.70122443 191.1107
## 80 north 3.67051110 200.8936
## 81 north 7.85249640 209.1958
## 82 north -1.51491090 187.4470
## 83 north 2.67847482 211.7307
## 84 north 5.68866775 226.5069
## 85 north 5.19163644 236.6999
## 86 north 9.31489156 198.0941
## 87 north 2.85780679 194.7594
## 88 north 2.87617442 219.2390
## 89 north 4.16252301 180.4928
## 90 north 0.27078522 205.6564
## 91 north 3.65712513 185.7367
## 92 north 4.96548874 199.8122
## 93 north 5.96893595 225.6065
## 94 north 5.61514824 225.9268
## 95 north 6.28499080 209.4649
## 96 north 4.04724998 197.7758
## 97 north 1.82004947 205.1500
## 98 north 5.67537861 195.0937
## 99 north 6.85551020 195.6574
## 100 north 9.41594914 215.9220
## 101 north 3.96104661 216.1778
## 102 north 7.00760235 203.2780
## 103 north 3.62800009 210.3994
## 104 north 3.39692400 179.0347
## 105 north 1.15958432 211.9756
## 106 north 6.77996583 189.4436
## 107 north -0.08031024 207.5637
## 108 north 6.76772303 178.6370
## 109 north 6.05353313 213.8915
## 110 north 5.08037439 179.3997
## 111 north 3.46270808 205.0344
## 112 north 4.15427207 194.3283
## 113 north 4.28888287 196.8492
## 114 north 8.63482660 207.5470
## 115 north 5.95814464 193.2245
## 116 north 6.92388800 201.3397
## 117 north 8.33620585 209.5271
## 118 north 5.77614175 205.4799
## 119 north 8.18747193 201.8910
## 120 north 3.49967230 210.7725
## 121 north 5.56944249 201.9818
## 122 north 0.75331385 210.1283
## 123 north 5.24579288 166.9091
## 124 north 4.08170486 227.4588
## 125 north 8.60292624 205.9169
## 126 north 3.80011824 198.4088
## 127 north 5.27915521 228.2941
## 128 north 2.80488116 189.6525
## 129 north 7.87140185 211.9908
## 130 north 2.79198422 188.2184
## 131 north 3.07213044 201.8008
## 132 north 4.04246494 205.1939
## 133 north 3.95947783 199.8934
## 134 north 6.60095655 214.8730
## 135 north 2.53431149 209.6295
## 136 north 6.20915810 193.7761
## 137 north 4.53192977 208.0766
## 138 north 7.57207812 220.7732
## 139 north 3.10439961 184.8742
## 140 north 4.54237409 190.0109
## 141 north 6.38254542 214.1735
## 142 north 2.75364176 211.8258
## 143 north 4.10631334 199.3361
## 144 north 6.69041680 215.1493
## 145 north 3.79570907 181.5531
## 146 north 6.87968938 207.4696
## 147 north 6.95280906 207.4188
## 148 north 8.84162379 173.2851
## 149 north 4.00403936 194.2821
## 150 north 5.28784227 206.5448
## 151 north 3.90455087 205.3801
## 152 north 4.11519281 183.1516
## 153 north 4.02379206 203.4666
## 154 north 6.01355517 193.6075
## 155 north 10.99789322 203.6429
## 156 north 4.96840410 187.0478
## 157 north 3.35638021 215.9436
## 158 north 6.95448611 203.9937
## 159 north 2.86894693 182.8543
## 160 north 6.94391499 207.4277
## 161 north 2.48442865 184.9767
## 162 north 3.00440654 222.8249
## 163 north 6.65094070 197.0939
## 164 north -0.32778618 210.7967
## 165 north 6.81273862 185.1083
## 166 north 2.00341661 229.7839
## 167 north 8.18101618 183.5018
## 168 north 1.21041391 174.5245
## 169 north 10.13959256 190.0819
## 170 north 5.01887698 190.8763
## 171 north 6.78907091 229.5399
## 172 north 3.50610189 170.1822
## 173 north 3.80229246 194.7034
## 174 north 3.27171318 203.6960
## 175 north 4.68316082 200.2240
## 176 north 6.69736792 192.8031
## 177 north 5.59618836 208.8882
## 178 north 6.27197340 211.0752
## 179 north 2.27572534 202.1307
## 180 north 7.75690787 194.5330
## 181 north -0.71738981 194.6796
## 182 north 10.31632150 187.2472
## 183 north 1.94622072 191.0000
## 184 north 7.83263608 206.0180
## 185 north 6.55242141 217.0659
## 186 north 3.73411005 216.0448
## 187 north 7.23676252 193.0772
## 188 north 6.36690863 205.8321
## 189 north 2.24512086 194.4072
## 190 north 0.25346966 227.9106
## 191 north 3.41398190 199.6810
## 192 north 6.73240095 215.2479
## 193 north 8.81565031 211.0246
## 194 north 8.14238973 199.0431
## 195 north 9.64932656 185.5875
## 196 north 4.96895947 195.7345
## 197 north 2.13841601 208.7928
## 198 north 2.71117581 198.6714
## 199 north 7.56479344 199.8848
## 200 north 7.40296388 187.1811
##
## [[2]]
## sites a b
## 1 east 3.72016013 209.9161
## 2 east 5.13088487 214.4626
## 3 east 1.53510164 173.4956
## 4 east 5.50461465 212.6347
## 5 east 1.19662149 200.0744
## 6 east 5.45216057 206.9543
## 7 east 4.90453638 217.7427
## 8 east 2.17137255 229.9014
## 9 east 4.73858864 201.0606
## 10 east 6.57457727 171.7150
## 11 east 3.73682205 181.5730
## 12 east 10.82193432 199.0877
## 13 east -0.40136763 178.9760
## 14 east 0.61488381 185.6988
## 15 east 5.08554254 166.3881
## 16 east 4.19689229 182.1598
## 17 east 4.27539376 185.5325
## 18 east 6.96379738 222.9917
## 19 east -0.01168290 210.6243
## 20 east 4.38265238 219.9425
## 21 east -1.47528546 238.9212
## 22 east 6.70606638 194.4868
## 23 east 4.10903790 229.6915
## 24 east 0.08118244 191.1877
## 25 east 5.43430004 177.0469
## 26 east 4.37851278 183.7355
## 27 east 10.13675108 196.3185
## 28 east 5.40403682 189.8261
## 29 east 7.96063137 200.8467
## 30 east 3.56003464 188.7158
## 31 east 5.96989709 207.9850
## 32 east 2.98471652 227.2813
## 33 east 7.95900185 186.5505
## 34 east 7.12941375 169.5934
## 35 east 5.06639528 197.1226
## 36 east 5.19288911 204.8934
## 37 east 4.71625167 224.0586
## 38 east 6.82300314 195.3002
## 39 east 7.46632615 171.8016
## 40 east 4.26484628 192.3472
## 41 east 6.49579155 235.9022
## 42 east 7.60361674 180.7717
## 43 east 3.76312916 203.7310
## 44 east 1.74762798 210.8561
## 45 east 5.91727832 200.3069
## 46 east 1.84228254 200.9787
## 47 east 6.53122721 165.7946
## 48 east 7.93344968 190.6416
## 49 east 8.48558365 207.7773
## 50 east 1.62658202 173.3319
## 51 east 7.35873675 200.5001
## 52 east 7.52581666 221.6814
## 53 east 5.68787511 202.7177
## 54 east 10.73344739 212.8598
## 55 east 5.39811378 188.9067
## 56 east 5.24237497 190.1318
## 57 east 5.70504455 197.6331
## 58 east 9.66401698 193.6296
## 59 east 1.04267198 177.8896
## 60 east 3.86971826 195.3268
## 61 east 1.54167809 176.6392
## 62 east 3.27493773 190.4924
## 63 east 6.39470376 199.1785
## 64 east 1.27178195 177.1628
## 65 east 3.45115576 216.2968
## 66 east 6.68245558 173.1788
## 67 east 1.82226244 196.8421
## 68 east 3.94626185 162.0177
## 69 east 5.42766205 201.5084
## 70 east 4.56018804 198.1869
## 71 east 6.10319608 206.4463
## 72 east 3.21737029 204.1251
## 73 east 6.01800637 190.1032
## 74 east 2.07604960 209.9678
## 75 east 5.48229331 164.0101
## 76 east 5.06820441 218.3148
## 77 east 5.62883539 172.0200
## 78 east 4.03976383 215.4354
## 79 east 6.86643566 215.2260
## 80 east 5.70130774 177.2374
## 81 east 7.99110975 187.6072
## 82 east 6.50615946 202.0275
## 83 east 4.55938620 211.6568
## 84 east 2.16658679 191.3032
## 85 east 3.17088875 208.1117
## 86 east 1.43189124 211.0245
## 87 east 2.33910682 205.0671
## 88 east 4.32433579 185.6769
## 89 east 5.67239242 192.2404
## 90 east 3.01713212 193.0915
## 91 east 2.94784798 182.0689
## 92 east 4.03757772 215.6875
## 93 east 4.18061106 207.9150
## 94 east 4.03509455 203.8432
## 95 east 6.37538702 225.1593
## 96 east -0.18993085 195.5606
## 97 east 4.60785504 204.7950
## 98 east 1.70409018 242.3897
## 99 east 9.35664162 197.6564
## 100 east 2.04173774 191.3517
## 101 east 4.50367824 211.6936
## 102 east 6.21905248 186.3960
## 103 east 5.97509132 239.1247
## 104 east 6.65874184 203.9751
## 105 east 2.68478846 186.0509
## 106 east 3.59704381 217.0706
## 107 east 5.42653017 195.3865
## 108 east 3.86273678 199.5981
## 109 east 4.30921787 192.9461
## 110 east 1.34261542 215.6577
## 111 east 0.60322724 177.9393
## 112 east 4.06637286 214.6128
## 113 east 5.07168009 177.8613
## 114 east 9.55099152 183.8408
## 115 east 1.51772975 216.9734
## 116 east 4.16163751 187.0027
## 117 east 7.88925850 207.5964
## 118 east 8.28106649 188.9622
## 119 east 3.47804973 190.6660
## 120 east 8.05459027 187.6738
## 121 east 5.87025192 179.5486
## 122 east 8.45133951 219.7200
## 123 east 9.55442387 190.7506
## 124 east 6.97406751 205.8840
## 125 east 2.99750469 178.5609
## 126 east 4.96459265 180.7047
## 127 east 4.48309988 198.2063
## 128 east 2.54484291 205.5088
## 129 east 5.28998350 207.1058
## 130 east -0.86425613 210.3008
## 131 east 5.94112566 197.9609
## 132 east 3.35998227 196.3211
## 133 east -2.85149670 223.2451
## 134 east 8.22610718 175.6545
## 135 east 6.39658253 222.0074
## 136 east 6.88707548 172.0415
## 137 east 8.29814256 179.8947
## 138 east 7.62508393 169.1722
## 139 east 2.47898360 221.4002
## 140 east 5.11012965 193.1726
## 141 east 8.14083180 213.9717
## 142 east 2.80469362 189.1226
## 143 east 2.14558984 193.5614
## 144 east 5.18126878 188.0360
## 145 east 5.49545104 214.8021
## 146 east 3.22011960 224.4559
## 147 east 6.35242234 188.1750
## 148 east 3.94925220 206.5127
## 149 east 3.58221614 202.9410
## 150 east 3.01190605 185.0080
## 151 east 6.97259104 194.2681
## 152 east 6.75894355 193.3239
## 153 east 6.89196540 212.9263
## 154 east 1.02521349 210.1194
## 155 east 7.26567133 209.7485
## 156 east 5.13138538 189.1985
## 157 east 7.65738348 203.8611
## 158 east 3.78140232 198.3208
## 159 east 2.51053069 195.5963
## 160 east 7.52619860 183.0724
## 161 east 3.46480566 172.3398
## 162 east 7.64025067 175.8898
## 163 east 6.69619868 186.8883
## 164 east 0.67025594 190.3853
## 165 east 5.04232972 184.4287
## 166 east 4.46175063 204.5830
## 167 east 1.86659299 209.7079
## 168 east 6.03408460 197.6708
## 169 east 8.42008621 173.3325
## 170 east 5.21756513 197.1393
## 171 east 6.89266137 200.6372
## 172 east 3.46540840 206.2990
## 173 east 4.13335629 198.2897
## 174 east 6.60116592 208.9647
## 175 east 7.06384282 198.0748
## 176 east 3.63718453 220.4648
## 177 east 5.47066390 225.9385
## 178 east 5.79155678 194.5469
## 179 east 7.96584696 182.1909
## 180 east -0.80667335 195.5736
## 181 east 3.48733200 175.7082
## 182 east 3.62369184 195.4429
## 183 east 0.67997937 205.0849
## 184 east 6.38083478 182.5563
## 185 east 8.94267854 178.0308
## 186 east 4.29310717 234.3965
## 187 east 1.71312604 208.5741
## 188 east 6.63280680 204.2930
## 189 east 8.24156716 218.9509
## 190 east 4.15289012 189.8785
## 191 east 5.12757448 203.6599
## 192 east 8.12451241 189.5466
## 193 east -1.05196989 214.1400
## 194 east 8.82960128 196.8573
## 195 east 6.31219934 196.7235
## 196 east 5.76602566 200.4849
## 197 east 4.49722427 195.1623
## 198 east 0.44504568 196.7081
## 199 east -0.94635231 184.4290
## 200 east 8.31167545 204.4735
##
## [[3]]
## sites a b
## 1 west 5.3256589 179.2464
## 2 west 5.2470133 202.5319
## 3 west 9.3112963 207.6849
## 4 west 7.7564026 185.9045
## 5 west 4.9740910 187.0437
## 6 west 1.4275952 203.2284
## 7 west 4.4732843 191.8531
## 8 west 4.9180938 162.7445
## 9 west 7.7358545 206.1007
## 10 west 2.7368686 199.5858
## 11 west 6.3464957 203.8673
## 12 west 6.4259708 197.5638
## 13 west 1.9877934 222.1702
## 14 west 3.9030158 188.8331
## 15 west 6.7371233 194.9487
## 16 west 2.5431881 185.5118
## 17 west 2.2479582 190.8503
## 18 west 3.3137443 189.0259
## 19 west 1.9306793 211.6140
## 20 west 3.7968789 206.1301
## 21 west 0.6208838 199.4242
## 22 west 8.5373300 169.1812
## 23 west 5.4756696 203.9246
## 24 west 8.2195918 180.5977
## 25 west 5.3608978 196.6755
## 26 west 3.3012410 164.1644
## 27 west 5.2857836 219.6314
## 28 west 2.1806893 213.8557
## 29 west 9.0081764 201.8924
## 30 west 4.8860152 192.7243
## 31 west 4.1535765 192.7810
## 32 west 3.1913195 189.3715
## 33 west 5.1536190 215.2372
## 34 west 3.6699102 202.8812
## 35 west 5.7963193 197.0043
## 36 west 7.6198877 197.4909
## 37 west 2.5051567 215.2432
## 38 west 5.0557590 215.6835
## 39 west 5.8437566 212.2571
## 40 west 7.2653858 212.5379
## 41 west 6.0466729 191.5955
## 42 west 3.5184348 243.0618
## 43 west -1.6614004 198.8176
## 44 west 8.6291531 200.4626
## 45 west -0.5632534 191.3006
## 46 west 2.5791048 195.9896
## 47 west 7.8892781 210.9722
## 48 west 1.6315456 214.6730
## 49 west 5.0798045 190.3256
## 50 west 3.4655422 194.3756
## 51 west 4.3597664 185.0181
## 52 west 5.2290641 191.3582
## 53 west 4.4124791 207.6325
## 54 west 4.1022755 214.7878
## 55 west 6.3277647 182.9928
## 56 west 10.9719692 206.8765
## 57 west 4.1191181 204.0071
## 58 west 5.0063491 215.9572
## 59 west 3.5259178 199.0942
## 60 west 4.0252352 200.0165
## 61 west 5.0384185 198.6601
## 62 west 5.3814817 198.0705
## 63 west 5.0537472 174.6802
## 64 west 7.7512607 190.5322
## 65 west 3.9020428 205.1107
## 66 west 4.1929790 204.4935
## 67 west 7.6690956 181.4939
## 68 west 4.2126015 170.9541
## 69 west 3.4252246 193.0098
## 70 west 6.8526373 187.8518
## 71 west 1.9686668 199.1581
## 72 west 5.3740409 199.2413
## 73 west 2.9841326 192.0801
## 74 west 6.1680922 202.8486
## 75 west 0.3919878 215.8896
## 76 west 5.6674975 202.1121
## 77 west 1.1125887 187.0941
## 78 west 7.2528032 183.4112
## 79 west 8.2748733 221.1579
## 80 west 6.0860660 187.2097
## 81 west 5.4450005 181.5276
## 82 west 3.5772343 167.9062
## 83 west 6.2185167 190.1867
## 84 west 6.9883765 190.1886
## 85 west 1.3596066 192.6370
## 86 west 3.5363650 208.2194
## 87 west 2.2765233 208.2318
## 88 west 8.2557153 217.6347
## 89 west 6.3889538 209.6606
## 90 west 6.5975434 204.6703
## 91 west 5.6435620 161.1661
## 92 west -1.0083184 200.5714
## 93 west 4.9747392 205.3380
## 94 west 3.6190135 188.1405
## 95 west 7.9753635 196.3902
## 96 west 6.4006031 186.4251
## 97 west 0.1702897 201.9280
## 98 west 6.4181888 208.5626
## 99 west 6.6413781 205.4258
## 100 west 7.8837589 192.1855
## 101 west 3.4637789 179.0128
## 102 west 6.7626579 169.2679
## 103 west 7.0213024 223.1613
## 104 west 5.4935222 184.1895
## 105 west 2.6737831 187.1252
## 106 west 8.1193014 215.7254
## 107 west 7.1286179 192.8988
## 108 west 2.2631930 189.2491
## 109 west 2.5437319 171.6542
## 110 west 6.5994162 183.2825
## 111 west 5.8558181 195.5572
## 112 west 8.7374504 207.6381
## 113 west 0.7009716 183.0748
## 114 west 5.3740624 201.8526
## 115 west 2.9436955 216.8719
## 116 west 4.8184737 205.7671
## 117 west 1.8295902 190.8339
## 118 west 8.3459802 221.1544
## 119 west 5.9006586 193.5228
## 120 west 4.7988829 204.5484
## 121 west 7.4644036 217.7364
## 122 west 7.0767061 217.4536
## 123 west 5.0234361 202.8020
## 124 west 1.3835158 180.5131
## 125 west 6.5222442 224.0175
## 126 west 3.0159601 224.4901
## 127 west 2.1120192 204.9290
## 128 west 1.1243710 201.8936
## 129 west 6.9986752 201.9561
## 130 west 1.8055075 202.0153
## 131 west 3.2007723 204.3717
## 132 west 2.9892017 204.7491
## 133 west 8.8766576 225.2825
## 134 west 7.6923098 204.0784
## 135 west 1.4004392 214.6737
## 136 west 5.1303130 187.7600
## 137 west 4.7990013 212.1943
## 138 west 4.5889251 194.8094
## 139 west 1.0822897 222.1995
## 140 west 5.5360273 199.0096
## 141 west 2.2109959 203.0456
## 142 west 4.0463270 208.9316
## 143 west 2.2067115 190.5594
## 144 west 4.1303634 194.6416
## 145 west 6.5241155 224.0110
## 146 west 1.9790618 217.2254
## 147 west 5.6658704 210.4717
## 148 west 6.3776986 197.8850
## 149 west 4.6535473 203.7949
## 150 west 4.4395603 185.0321
## 151 west 4.5231259 204.1371
## 152 west 3.3970626 215.0517
## 153 west 5.4465091 208.3618
## 154 west 4.6858809 187.1637
## 155 west 3.9807158 186.1565
## 156 west 13.9963628 157.8218
## 157 west 1.5750825 214.1742
## 158 west 3.8043131 221.2954
## 159 west 5.4859435 224.7824
## 160 west 6.2000206 231.1361
## 161 west 5.0076446 212.6378
## 162 west 9.3483198 212.6633
## 163 west 3.2612141 199.4552
## 164 west 5.8392508 220.5975
## 165 west 6.0937489 186.7038
## 166 west 6.2274584 196.4363
## 167 west 4.3433645 195.2027
## 168 west 4.3405896 205.0286
## 169 west 1.0759883 218.7616
## 170 west 6.7879049 204.7116
## 171 west 0.3924562 187.3598
## 172 west -0.1042265 165.3180
## 173 west 3.2421369 192.4175
## 174 west 3.4615337 189.9494
## 175 west 5.8148074 189.3206
## 176 west 6.5427905 201.6653
## 177 west 3.6534559 195.9637
## 178 west 5.9194793 176.8706
## 179 west 3.4968232 205.9313
## 180 west 5.0583629 199.9967
## 181 west -1.2121577 182.0949
## 182 west 2.3592949 198.0897
## 183 west 6.4434052 199.9378
## 184 west 3.8128463 194.4464
## 185 west 3.3875471 206.5384
## 186 west 5.3125715 184.8190
## 187 west 6.0740455 206.4128
## 188 west 5.9650735 189.2911
## 189 west 5.8472477 190.3797
## 190 west 8.5042420 194.3233
## 191 west 3.5285498 215.7122
## 192 west 3.0990339 166.0223
## 193 west 10.5414437 196.0819
## 194 west 6.0740939 200.4535
## 195 west 7.2395845 178.2125
## 196 west 8.5479798 219.8218
## 197 west 5.6850631 198.8678
## 198 west 3.8120197 202.5325
## 199 west 1.8474705 180.1095
## 200 west 4.6744996 191.9134
# Map over the models to look at the relationship of a vs b
list_of_df %>%
map(~ lm(a ~ b, data = .)) %>%
map(summary)## [[1]]
##
## Call:
## lm(formula = a ~ b, data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.5585 -1.6848 -0.0443 1.7994 6.6383
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.616317 2.253431 2.492 0.0135 *
## b -0.003055 0.011156 -0.274 0.7845
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.491 on 198 degrees of freedom
## Multiple R-squared: 0.0003787, Adjusted R-squared: -0.00467
## F-statistic: 0.075 on 1 and 198 DF, p-value: 0.7845
##
##
## [[2]]
##
## Call:
## lm(formula = a ~ b, data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.1219 -1.5046 0.1634 1.8899 6.2464
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.92893 2.23737 3.991 9.27e-05 ***
## b -0.02087 0.01126 -1.853 0.0653 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.558 on 198 degrees of freedom
## Multiple R-squared: 0.01705, Adjusted R-squared: 0.01209
## F-statistic: 3.435 on 1 and 198 DF, p-value: 0.06532
##
##
## [[3]]
##
## Call:
## lm(formula = a ~ b, data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.4562 -1.4734 0.1834 1.5866 9.3445
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.101665 2.373359 1.728 0.0855 .
## b 0.003486 0.011906 0.293 0.7700
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.43 on 198 degrees of freedom
## Multiple R-squared: 0.0004329, Adjusted R-squared: -0.004615
## F-statistic: 0.08574 on 1 and 198 DF, p-value: 0.77
# Pull out the director element of sw_films in a list and character vector
map(sw_films, ~.x[["director"]])## $`A New Hope`
## [1] "George Lucas"
##
## $`Attack of the Clones`
## [1] "George Lucas"
##
## $`The Phantom Menace`
## [1] "George Lucas"
##
## $`Revenge of the Sith`
## [1] "George Lucas"
##
## $`Return of the Jedi`
## [1] "Richard Marquand"
##
## $`The Empire Strikes Back`
## [1] "Irvin Kershner"
##
## $`The Force Awakens`
## [1] "J. J. Abrams"
## A New Hope Attack of the Clones The Phantom Menace
## "George Lucas" "George Lucas" "George Lucas"
## Revenge of the Sith Return of the Jedi The Empire Strikes Back
## "George Lucas" "Richard Marquand" "Irvin Kershner"
## The Force Awakens
## "J. J. Abrams"
# Compare outputs when checking if director is George Lucas
map(sw_films, ~.x[["director"]] == "George Lucas")## $`A New Hope`
## [1] TRUE
##
## $`Attack of the Clones`
## [1] TRUE
##
## $`The Phantom Menace`
## [1] TRUE
##
## $`Revenge of the Sith`
## [1] TRUE
##
## $`Return of the Jedi`
## [1] FALSE
##
## $`The Empire Strikes Back`
## [1] FALSE
##
## $`The Force Awakens`
## [1] FALSE
## A New Hope Attack of the Clones The Phantom Menace
## TRUE TRUE TRUE
## Revenge of the Sith Return of the Jedi The Empire Strikes Back
## TRUE FALSE FALSE
## The Force Awakens
## FALSE
## $`A New Hope`
## [1] 4
##
## $`Attack of the Clones`
## [1] 2
##
## $`The Phantom Menace`
## [1] 1
##
## $`Revenge of the Sith`
## [1] 3
##
## $`Return of the Jedi`
## [1] 6
##
## $`The Empire Strikes Back`
## [1] 5
##
## $`The Force Awakens`
## [1] 7
## A New Hope Attack of the Clones The Phantom Menace
## 4 2 1
## Revenge of the Sith Return of the Jedi The Empire Strikes Back
## 3 6 5
## The Force Awakens
## 7
## A New Hope Attack of the Clones The Phantom Menace
## 4 2 1
## Revenge of the Sith Return of the Jedi The Empire Strikes Back
## 3 6 5
## The Force Awakens
## 7
Oftentimes we need to use multiple datasets stored as lists to answer questions. In this case we need to use map2 function to pull out information from each list and bring it together. If you have three or more lists you can use pmap. There is difference in how we use pmap compared to map or map2. The first difference is how we input lists. In pmap we need to create a list of lists we want to use as input. The second argument to pmap is a custom function argument. Inside the custom function is the names of the elements from our input list as the arguments. This allows us to use name of those lists as arguments instead of using .x or .y.
simdata <- map2(list_of_means, list_of_sd, ~data.frame(a = rnorm(mean=.x, n=200, sd=.y)
,b = rnorm(mean=200, n=200, sd=15)))
simdata <- pmap(inputs_list,
function(means, sd, samplesize)
data.frame(a = rnorm(mean=means, n=samplesize, sd=sd)))# List of 1, 2 and 3
means <- list(1, 2, 3)
# Create sites list
sites <- list("north", "west", "east")
# Map over two arguments: sites and means
list_of_files_map2 <- map2(sites, means, ~data.frame(sites = .x,
a = rnorm(mean = .y, n = 200, sd = (5/2))))
head(list_of_files_map2)## [[1]]
## sites a
## 1 north 1.201042075
## 2 north 1.924388004
## 3 north -1.259059559
## 4 north -0.623257471
## 5 north -2.357483619
## 6 north 0.756781574
## 7 north 4.387023953
## 8 north 4.945243426
## 9 north -0.024052202
## 10 north -0.606934011
## 11 north -0.945287083
## 12 north -0.645948642
## 13 north 2.609348741
## 14 north 0.360216380
## 15 north -2.865828044
## 16 north 1.057092026
## 17 north 3.036849468
## 18 north -1.964446954
## 19 north 2.609268199
## 20 north -1.312604107
## 21 north 0.049118669
## 22 north 5.682607943
## 23 north 2.008908889
## 24 north 2.847976574
## 25 north -1.886712670
## 26 north -0.342737324
## 27 north -0.510206056
## 28 north 1.839170877
## 29 north -2.126780561
## 30 north 0.718868001
## 31 north 0.174577909
## 32 north -2.150583809
## 33 north -3.982026598
## 34 north 4.335070491
## 35 north 6.604466102
## 36 north 1.646700270
## 37 north 1.757934995
## 38 north 2.404339775
## 39 north 4.982363131
## 40 north 4.534428249
## 41 north 3.415205552
## 42 north 1.869097190
## 43 north 1.198577761
## 44 north 2.768035611
## 45 north 2.748229467
## 46 north 1.962682724
## 47 north 2.411908295
## 48 north 3.460844985
## 49 north 5.099069367
## 50 north -0.254522023
## 51 north 1.673113441
## 52 north 2.333146958
## 53 north 4.440476618
## 54 north 4.116855053
## 55 north 0.700575547
## 56 north -0.971285575
## 57 north 3.103890703
## 58 north 0.002309598
## 59 north 6.291612911
## 60 north 3.519609097
## 61 north 2.057330542
## 62 north 5.257586252
## 63 north 0.222928618
## 64 north 2.118909814
## 65 north 3.209799423
## 66 north 3.400224680
## 67 north 4.989770973
## 68 north -1.361963653
## 69 north -0.827626105
## 70 north 2.126956289
## 71 north 3.027725142
## 72 north -1.055050102
## 73 north -0.912500745
## 74 north 0.791751437
## 75 north 4.348360569
## 76 north 1.171643937
## 77 north 0.277352912
## 78 north -3.055037169
## 79 north -0.882422561
## 80 north 1.146662981
## 81 north -2.610340666
## 82 north -0.710289834
## 83 north 4.386325533
## 84 north 0.755271857
## 85 north 1.706355101
## 86 north 2.130451389
## 87 north 0.155466440
## 88 north -0.612707861
## 89 north -1.362188691
## 90 north -3.248551843
## 91 north -0.550968485
## 92 north -0.513081543
## 93 north 0.331851667
## 94 north -2.860530785
## 95 north 0.827152870
## 96 north 0.633122756
## 97 north 1.451769440
## 98 north 1.531869736
## 99 north 3.458081330
## 100 north 2.951857970
## 101 north 0.036149258
## 102 north -2.993048960
## 103 north 0.874443248
## 104 north 1.009216260
## 105 north 2.450200013
## 106 north -0.149207216
## 107 north 1.222752870
## 108 north 0.410020582
## 109 north -1.697795193
## 110 north 3.843121310
## 111 north 0.480065097
## 112 north -0.033400560
## 113 north -4.124338200
## 114 north 3.448968575
## 115 north 4.548266370
## 116 north 0.869154847
## 117 north 3.052173350
## 118 north -3.190211752
## 119 north 0.813342466
## 120 north 5.378860305
## 121 north -1.990712721
## 122 north 2.218611435
## 123 north 4.810096309
## 124 north 1.456130523
## 125 north 1.200539258
## 126 north 1.335253455
## 127 north 1.601113128
## 128 north -2.673021381
## 129 north 2.553445336
## 130 north 0.402321483
## 131 north 6.705150419
## 132 north -3.394877400
## 133 north 2.939954279
## 134 north 0.306031686
## 135 north 1.110914441
## 136 north -0.229485041
## 137 north -0.514215391
## 138 north 0.220893002
## 139 north 0.654229875
## 140 north -0.653514756
## 141 north 2.747670449
## 142 north 3.530782716
## 143 north -0.445349660
## 144 north -3.007507365
## 145 north -0.834961381
## 146 north 4.570180133
## 147 north 4.635285509
## 148 north -3.365924204
## 149 north 1.932145936
## 150 north -3.367283511
## 151 north 1.005167456
## 152 north 1.109710292
## 153 north 3.889483260
## 154 north 1.364599702
## 155 north 3.518296550
## 156 north 3.113472263
## 157 north -0.480624185
## 158 north 1.893208057
## 159 north 2.308442797
## 160 north -0.922853828
## 161 north 4.003537326
## 162 north 0.557615089
## 163 north 1.400244777
## 164 north 1.637128611
## 165 north 5.804685489
## 166 north 5.779034729
## 167 north -2.637267998
## 168 north 1.106257193
## 169 north 6.615700757
## 170 north 4.869709813
## 171 north 0.173439847
## 172 north -2.264095178
## 173 north 1.531409946
## 174 north -4.410560350
## 175 north 0.756212423
## 176 north 4.663108116
## 177 north 3.172222058
## 178 north 1.570867901
## 179 north -0.785074267
## 180 north -1.831257156
## 181 north -1.846336823
## 182 north 0.601868177
## 183 north -3.751329803
## 184 north 2.655289363
## 185 north 4.267055387
## 186 north -2.124821062
## 187 north 1.033435591
## 188 north -0.544497931
## 189 north 1.676904904
## 190 north 3.211668872
## 191 north -0.953436019
## 192 north -1.062234517
## 193 north 1.571507133
## 194 north -0.854086324
## 195 north 1.953977473
## 196 north -0.665321717
## 197 north 5.306634726
## 198 north 3.816056689
## 199 north -1.910010926
## 200 north -1.680030280
##
## [[2]]
## sites a
## 1 west 2.53013294
## 2 west 7.31481447
## 3 west -0.22935737
## 4 west 2.20915439
## 5 west 0.61943402
## 6 west 1.23355286
## 7 west 0.20316611
## 8 west 0.95760308
## 9 west -0.50368855
## 10 west 1.57688661
## 11 west 1.99734675
## 12 west 1.91053974
## 13 west 7.66316221
## 14 west 3.46536030
## 15 west 4.45288893
## 16 west 0.61806449
## 17 west 4.07173403
## 18 west 1.64808457
## 19 west 0.09864664
## 20 west 4.84666871
## 21 west 4.34335403
## 22 west 3.54886727
## 23 west 3.09252986
## 24 west 4.16032098
## 25 west 2.65558297
## 26 west 4.76816815
## 27 west 3.18616301
## 28 west -1.87333681
## 29 west 2.93388660
## 30 west 3.95077741
## 31 west 4.74264946
## 32 west 1.15871508
## 33 west 1.15997115
## 34 west 2.63072470
## 35 west 2.42248550
## 36 west 2.62748005
## 37 west 2.98546001
## 38 west 5.76164611
## 39 west 3.37366623
## 40 west 0.10982056
## 41 west 4.26289112
## 42 west 1.31219265
## 43 west 0.49374456
## 44 west 5.35326175
## 45 west 0.96187258
## 46 west 1.18298461
## 47 west 5.10098469
## 48 west 4.82693111
## 49 west 6.14429835
## 50 west 1.22999807
## 51 west 3.37218938
## 52 west 6.98722585
## 53 west 0.99691521
## 54 west 4.12877849
## 55 west 1.03135911
## 56 west 0.86339196
## 57 west 0.53211469
## 58 west 2.62476841
## 59 west -0.22409637
## 60 west 1.16909925
## 61 west 3.52255982
## 62 west -1.94083115
## 63 west 0.94329926
## 64 west 2.51317290
## 65 west 7.33756588
## 66 west 2.43371622
## 67 west 2.19658571
## 68 west 0.34327074
## 69 west 0.47535093
## 70 west -1.68672366
## 71 west -1.33782154
## 72 west 5.80539270
## 73 west 5.62598674
## 74 west 2.21809832
## 75 west 1.21427922
## 76 west 0.80872523
## 77 west -0.48621678
## 78 west 2.65831525
## 79 west 3.77714528
## 80 west 2.09677667
## 81 west 2.47548582
## 82 west 3.08412712
## 83 west 1.26709673
## 84 west 0.55582500
## 85 west 0.56118208
## 86 west -0.52099318
## 87 west 1.23166187
## 88 west 2.30955699
## 89 west 7.20976008
## 90 west 3.14867592
## 91 west 2.61292547
## 92 west -0.14750892
## 93 west 2.34457430
## 94 west 1.91441191
## 95 west 1.53314095
## 96 west 1.38353233
## 97 west 0.36420783
## 98 west 3.86044324
## 99 west 3.17790720
## 100 west -1.32272922
## 101 west 4.82838234
## 102 west 4.93736755
## 103 west 5.65819398
## 104 west -3.00386728
## 105 west 1.06815443
## 106 west -0.18035300
## 107 west -0.20140237
## 108 west -1.94130130
## 109 west 6.57575182
## 110 west 0.28248838
## 111 west 1.55152051
## 112 west 4.29979008
## 113 west 3.55284910
## 114 west -1.30288175
## 115 west 9.72167287
## 116 west -3.99496499
## 117 west 3.22456895
## 118 west 2.64239155
## 119 west -0.00113236
## 120 west 0.73569655
## 121 west -2.90186223
## 122 west 0.67477064
## 123 west -0.24542862
## 124 west 8.06559684
## 125 west 5.59031496
## 126 west 5.64992644
## 127 west 2.52150080
## 128 west 4.27668557
## 129 west -0.22113368
## 130 west 3.21691657
## 131 west 5.27835892
## 132 west -5.94852669
## 133 west 0.50729528
## 134 west 6.21269364
## 135 west 2.45194841
## 136 west 6.81851037
## 137 west 4.37162329
## 138 west 1.08544893
## 139 west -0.92408539
## 140 west -1.73566526
## 141 west -0.20353871
## 142 west 4.09194235
## 143 west 2.20103652
## 144 west 3.90381791
## 145 west 1.26776388
## 146 west 1.32675681
## 147 west 2.98980297
## 148 west 5.49323410
## 149 west 2.36405481
## 150 west 1.19512478
## 151 west 3.05424601
## 152 west 0.49705636
## 153 west 2.34170286
## 154 west 4.25699303
## 155 west 5.01375694
## 156 west 1.04724958
## 157 west -0.33298458
## 158 west 2.51921200
## 159 west 1.85559477
## 160 west -0.68096405
## 161 west 5.97715093
## 162 west 2.36474030
## 163 west 0.85791068
## 164 west 3.12286986
## 165 west 1.79208136
## 166 west 5.24882711
## 167 west 2.93612778
## 168 west 2.43499948
## 169 west 1.79781476
## 170 west 2.92001862
## 171 west 6.00920801
## 172 west 3.75227181
## 173 west 4.21294227
## 174 west 1.06837219
## 175 west 1.55560267
## 176 west 2.95158243
## 177 west 2.31104818
## 178 west 0.63934949
## 179 west 2.42516589
## 180 west 3.28591726
## 181 west 2.00760567
## 182 west 4.77191260
## 183 west 2.90666015
## 184 west 1.43640396
## 185 west -0.18606765
## 186 west 2.03426725
## 187 west 3.84400578
## 188 west 1.36327074
## 189 west -0.43189009
## 190 west 4.09937041
## 191 west 1.53605354
## 192 west 1.74469667
## 193 west 1.67792490
## 194 west -2.10861140
## 195 west 3.55156929
## 196 west 0.26932483
## 197 west 3.31673125
## 198 west 4.99518135
## 199 west -1.64565110
## 200 west 3.21781023
##
## [[3]]
## sites a
## 1 east 1.20501747
## 2 east -0.13082587
## 3 east 3.57228633
## 4 east 1.30970449
## 5 east 5.43703568
## 6 east 5.17765415
## 7 east 1.21774710
## 8 east 1.91804190
## 9 east 2.36518882
## 10 east 3.97289676
## 11 east 3.11571877
## 12 east 3.39947248
## 13 east 0.82778063
## 14 east 3.83505236
## 15 east -1.57514789
## 16 east 4.88165340
## 17 east 3.81781117
## 18 east 3.14187379
## 19 east 1.23726941
## 20 east 2.30080088
## 21 east 2.42017488
## 22 east 5.75445830
## 23 east 3.81862643
## 24 east 4.95558399
## 25 east 2.35119900
## 26 east 14.29937817
## 27 east 1.48695463
## 28 east 0.03814956
## 29 east 5.06254299
## 30 east 2.94588283
## 31 east -1.87008503
## 32 east 2.88699921
## 33 east 1.64596057
## 34 east 3.21729582
## 35 east 0.06221238
## 36 east -0.16950214
## 37 east -2.20744037
## 38 east 1.80860465
## 39 east 3.77797119
## 40 east -0.54164113
## 41 east 4.83847827
## 42 east -1.52457804
## 43 east 4.72929223
## 44 east 5.31767226
## 45 east 3.22545987
## 46 east 5.66608995
## 47 east 2.54195108
## 48 east -1.11647077
## 49 east -1.11771621
## 50 east 4.17976702
## 51 east -2.21556762
## 52 east 0.66896572
## 53 east -2.84155927
## 54 east 1.56570279
## 55 east 3.72242392
## 56 east -0.47010462
## 57 east -0.69411338
## 58 east 2.51136097
## 59 east 4.03796393
## 60 east 9.08801169
## 61 east 2.57847878
## 62 east -0.06851133
## 63 east 4.87633342
## 64 east 3.58242481
## 65 east 2.17318440
## 66 east 6.07221051
## 67 east 2.97248098
## 68 east 4.60444285
## 69 east 1.17339736
## 70 east 1.10077866
## 71 east 2.17662987
## 72 east 4.81936294
## 73 east 4.88474206
## 74 east 2.56959495
## 75 east 7.74482139
## 76 east 2.67725121
## 77 east -0.97433692
## 78 east 3.80334422
## 79 east 0.38452829
## 80 east 0.02403884
## 81 east 4.65278900
## 82 east 2.76169172
## 83 east 4.48904079
## 84 east 2.34026575
## 85 east 0.65184813
## 86 east 3.62037477
## 87 east 3.93546360
## 88 east 4.76539313
## 89 east 2.90077682
## 90 east 1.97365977
## 91 east -1.74734854
## 92 east 6.49015367
## 93 east -3.16520115
## 94 east 3.61365848
## 95 east -0.04409108
## 96 east 4.51719536
## 97 east 0.13326370
## 98 east 8.01099832
## 99 east 1.32283274
## 100 east 2.16588980
## 101 east 1.85057842
## 102 east -4.07187238
## 103 east 7.32369315
## 104 east -0.12090439
## 105 east 3.98638881
## 106 east 10.34292913
## 107 east 0.16390101
## 108 east 0.12683027
## 109 east 2.99750129
## 110 east 1.61959920
## 111 east 2.02293490
## 112 east 1.57722891
## 113 east 1.49520917
## 114 east 0.92701974
## 115 east -0.74460820
## 116 east 4.25862026
## 117 east 1.60324271
## 118 east 4.32095526
## 119 east 7.25739955
## 120 east 4.61552165
## 121 east 2.30058066
## 122 east 5.30624097
## 123 east 1.25255139
## 124 east 4.47988432
## 125 east 7.52858622
## 126 east 1.40932343
## 127 east 3.59940410
## 128 east 3.01727898
## 129 east 6.39015988
## 130 east 1.67064848
## 131 east 4.06642632
## 132 east 4.97471477
## 133 east 5.85506719
## 134 east 4.01123864
## 135 east 2.31465085
## 136 east 4.21950373
## 137 east 4.76073934
## 138 east 2.67732703
## 139 east 4.17010248
## 140 east 1.72450767
## 141 east 2.66562367
## 142 east 5.65957024
## 143 east 1.29202958
## 144 east 4.02208223
## 145 east 5.17042417
## 146 east 3.67199714
## 147 east 2.87933926
## 148 east -1.78416651
## 149 east 1.58403610
## 150 east 2.97040202
## 151 east 4.24488634
## 152 east 4.95937112
## 153 east 5.94545288
## 154 east 4.42323493
## 155 east 5.84288767
## 156 east 0.72989615
## 157 east 3.29435597
## 158 east 3.25511794
## 159 east 6.54213592
## 160 east 1.69979886
## 161 east 1.29567267
## 162 east 5.28213552
## 163 east 1.85190336
## 164 east 4.71702022
## 165 east 3.45389829
## 166 east 7.15422419
## 167 east 5.37220000
## 168 east 2.86582652
## 169 east 2.36296709
## 170 east 0.72512967
## 171 east 1.64374758
## 172 east -1.89614073
## 173 east 5.73224545
## 174 east -1.19114823
## 175 east 4.38098391
## 176 east 4.81763990
## 177 east 7.29629553
## 178 east 6.34064943
## 179 east 1.80891079
## 180 east 0.67173408
## 181 east 2.85263854
## 182 east 5.06744911
## 183 east 7.36452347
## 184 east 5.83753462
## 185 east -0.37490838
## 186 east 6.98783095
## 187 east -0.71327543
## 188 east 2.28163707
## 189 east 1.52908125
## 190 east 1.02355557
## 191 east 1.33645355
## 192 east 1.17449515
## 193 east 1.65642903
## 194 east 6.88881606
## 195 east 5.37370832
## 196 east -1.63738985
## 197 east 2.02411320
## 198 east 0.47893964
## 199 east 5.26574657
## 200 east 3.10569240
# Create a master list, a list of lists
pmapinputs <- list(sites = sites, means = means, sigma = sigma,
means2 = means2, sigma2 = sigma2)
# Map over the master list
list_of_files_pmap <- pmap(pmapinputs,
function(sites, means, means2, sigma, sigma2)
data.frame(sites = sites,
a = rnorm(mean = means, n = 200, sd = sigma),
b = rnorm(mean = means2, n = 200, sd = sigma2)))
head(list_of_files_pmap)When you try to use map over a list and one of the elements has a wrong data type then the map() function will not work.
In the below example we are using another function safely() inside of map to identify where in the list an issue is coming. By using safely argument otherwise we can tell safely how to deal with input that it does not expect. Supplying NA_real_ to the otherwise argument outputs NA when an unexpected input comes from the list element and then map will continue to the next element without breaking.
Using safely with map produces more complex output than just using map on its own. Each output list has a result element and an error element so that you can see where the errors are occurring and what they are.
Using safely our main purpose is to find what the errors are and which elements in the list caused them. Grouping together error messages can also make this easier. This can be done using transpose() function at the end of our workflow.
a <- list("unknown", 10) %>% map(safely(function(x) x * 10, otherwise = NA_real_))
# Using transpose
a <- list("unknown", 10) %>% map(safely(function(x) x * 10, otherwise = NA_real_)) %>% transpose()# Map safely over log
a <- list(-10, 1, 10, 0) %>%
map(safely(log, otherwise = NA_real_)) %>%
# Transpose the result
transpose()## Warning in .f(...): NaNs produced
## $result
## $result[[1]]
## [1] NaN
##
## $result[[2]]
## [1] 0
##
## $result[[3]]
## [1] 2.302585
##
## $result[[4]]
## [1] -Inf
##
##
## $error
## $error[[1]]
## NULL
##
## $error[[2]]
## NULL
##
## $error[[3]]
## NULL
##
## $error[[4]]
## NULL
## [[1]]
## [1] NaN
##
## [[2]]
## [1] 0
##
## [[3]]
## [1] 2.302585
##
## [[4]]
## [1] -Inf
## [[1]]
## NULL
##
## [[2]]
## NULL
##
## [[3]]
## NULL
##
## [[4]]
## NULL
# Load sw_people data
data(sw_people)
# Map over sw_people and pull out the height element
height_cm <- map(sw_people, "height") %>%
map(function(x){
ifelse(x == "unknown",NA,
as.numeric(x))
})# Map over sw_people and pull out the height element
# Set quiet = FALSE so that errors are printed.
height_ft <- map(sw_people , "height") %>%
map(safely(function(x){
x * 0.0328084
}, quiet = FALSE)) %>%
transpose()
# Print your list, the result element, and the error element
height_ft
height_ft[["result"]]
height_ft[["error"]]safely helps us in pointing where the issue is and what the issue is with messy data, possibly() helps us get past it and get on with our day. It is a good idea to diagnose your issue with safely and then replace it with possibly once the issue is resolved so that you can get the output you want and keep working. possibly will just return the results and not the error elements.
# Take the log of each element in the list
a <- list(-10, 1, 10, 0) %>%
map(possibly(function(x){
log(x)
},otherwise = NA_real_))## Warning in log(x): NaNs produced
# Create a piped workflow that returns double vectors
height_cm %>%
map_dbl(possibly(function(x){
# Convert centimeters to feet
x * 0.0328084
}, otherwise = NA_real_)) ## [1] 5.643045 5.479003 3.149606 6.627297 4.921260 5.839895 5.413386 3.182415
## [9] 6.003937 5.971129 6.167979 5.905512 7.480315 5.905512 5.675853 5.741470
## [17] 5.577428 5.905512 2.165354 5.577428 6.003937 6.561680 6.233596 5.807087
## [25] 5.741470 5.905512 4.921260 NA 2.887139 5.249344 6.332021 6.266404
## [33] 5.577428 6.430446 7.349082 6.758530 6.003937 4.494751 3.674541 6.003937
## [41] 5.347769 5.741470 5.905512 5.839895 3.083990 4.002625 5.347769 6.167979
## [49] 6.496063 6.430446 5.610236 6.036746 6.167979 8.661418 6.167979 6.430446
## [57] 6.069554 5.150919 6.003937 6.003937 5.577428 5.446194 5.413386 6.332021
## [65] 6.266404 6.003937 5.511811 6.496063 7.513124 6.988189 5.479003 2.591864
## [73] 3.149606 6.332021 6.266404 5.839895 7.086614 7.677166 6.167979 5.839895
## [81] 6.758530 NA NA NA NA NA 5.413386
walk() helps make purrr cleaner and helps us create outputs that are more human readable.Regular lists output is bracketed elements and isn’t very easy to read. walk outputs same information as normal lists do but it is much more compact and easier to search through. It works like map and needs both .x and .f argument. Many times you want to plot multiple graphs that are part of list, this is the scenario where you can use walk to plot the graphs without printing the list elements.
## [[1]]
## [1] -10
##
## [[2]]
## [1] 1
##
## [[3]]
## [1] 10
## [1] -10
## [1] 1
## [1] 10
# Load the gap_split data
data(gap_split)
# Map over the first 10 elements of gap_split
plots <- map2(gap_split[1:10],
names(gap_split[1:10]),
~ ggplot(.x, aes(year, lifeExp)) +
geom_line() +
labs(title = .y))
# Object name, then function name
walk(plots, print)Setting Names using set_names function for unnamed lists.
#Setting Names
sw_films <- sw_films %>%
set_names(map_chr(sw_films,"title"))
# Sort by episode Id and set name
map_chr(sw_films, ~.x[["episode_id"]]) %>%
set_names(map_chr(sw_films,"title")) %>%
sort()## The Phantom Menace Attack of the Clones Revenge of the Sith
## "1" "2" "3"
## A New Hope The Empire Strikes Back Return of the Jedi
## "4" "5" "6"
## The Force Awakens
## "7"
## NULL
## [[1]]
## [1] "Gábor Csárdi"
##
## [[2]]
## [1] "Jennifer (Jenny) Bryan"
##
## [[3]]
## [1] "Jeff L."
##
## [[4]]
## [1] "Julia Silge"
##
## [[5]]
## [1] "Thomas J. Leeper"
##
## [[6]]
## [1] "Maëlle Salmon"
# Name gh_users with the names of the users
gh_users_named <- gh_users %>%
set_names(map_chr(gh_users, "name"))
# Check gh_repos structure
# str(gh_repos)
# Name gh_repos with the names of the repo owner
gh_repos_named <- gh_repos %>%
map_chr(~ .[[1]]$owner$login) %>%
set_names(gh_repos, .)# Determine who joined github first
map_chr(gh_users, ~.x[["created_at"]]) %>%
set_names(map_chr(gh_users,"name")) %>%
sort()## Jennifer (Jenny) Bryan Gábor Csárdi Jeff L.
## "2011-02-03T22:37:41Z" "2011-03-09T17:29:25Z" "2012-03-24T18:16:43Z"
## Thomas J. Leeper Maëlle Salmon Julia Silge
## "2013-02-07T21:07:00Z" "2014-08-05T08:10:04Z" "2015-05-19T02:51:23Z"
## [1] TRUE TRUE TRUE TRUE TRUE TRUE
# Determine who has the most public repositories
map_int(gh_users, ~.x[["public_repos"]]) %>%
set_names(map_chr(gh_users, "name")) %>%
sort()## Julia Silge Maëlle Salmon Gábor Csárdi
## 26 31 52
## Jeff L. Thomas J. Leeper Jennifer (Jenny) Bryan
## 67 99 168
Sometimes the problems you are trying to solve using lists are complex(ex. the variable name we are interested in is buried/nested inside multiple lists).
To extract data from a list that is inside a list we will use nested map function.
# Summary Statistics
# select_if will return only columns that match the criteria we put inside the function
# In the below example we are returning the columns that are numeric.
bird_measurements %>%
map_df(~ data_frame(
weight = .x[["weight"]],
wing_length = .x[["wing length"]],
taxa = "bird")) %>%
select_if(is.numeric) %>%
summary(.x)# Map over gh_repos to generate numeric output
map(gh_repos,
~map_dbl(.x,
~.x[["size"]])) %>%
# Grab the largest element
map(~max(.x))## [[1]]
## [1] 39461
##
## [[2]]
## [1] 96325
##
## [[3]]
## [1] 374812
##
## [[4]]
## [1] 24070
##
## [[5]]
## [1] 558176
##
## [[6]]
## [1] 76455
ggplot requires dataframe as input. Then add layers using + sign. geom_*() shows graph type. Since ggplot requires data frame as input but our data is often stored as list we can use map_df to transform the data from a list into a data frame we need.
We can pipe that dataframe directly into ggplot code without the need of the intermediate objects.
birddf <- bird_measurements %>%
map_df(~ data_frame(
wing_length = .x[["wing length"]],
weight = .x[["weight"]])) %>%
ggplot(aes(x = weight,
y = wing_length))+
geom_point()# Scatter plot of public repos and followers
ggplot(data = gh_users_df,
aes(x = public_repos, y = followers))+
geom_point()
# Histogram of followers
gh_users_df %>%
ggplot(aes(x = followers))+
geom_histogram()# Create a dataframe with four columns
map_df(gh_users, `[`,
c("login", "name", "followers", "public_repos")) %>%
# Plot followers by public_repos
ggplot(.,
aes(x = followers, y = public_repos)) +
# Create scatter plots
geom_point()# Turn data into correct dataframe format
film_by_character <- tibble(filmtitle = map_chr(sw_films, "title")) %>%
mutate(filmtitle, characters = map(sw_films, "characters")) %>%
unnest()## Warning: `cols` is now required.
## Please use `cols = c(characters)`
# Pull out elements from sw_people
sw_characters <- map_df(sw_people, `[`, c("height","mass","name","url"))
# Join the two new objects
character_data <- inner_join(film_by_character, sw_characters, by = c("characters" = "url")) %>%
# Make sure the columns are numbers
mutate(height = as.numeric(height), mass = as.numeric(mass))## Warning: NAs introduced by coercion
## Warning: NAs introduced by coercion
# Plot the heights, faceted by film title
ggplot(character_data, aes(x = height)) +
geom_histogram(stat = "count") +
facet_wrap(~ filmtitle)## Warning: Ignoring unknown parameters: binwidth, bins, pad
## Warning: Removed 6 rows containing non-finite values (stat_count).
********************************************************************END**********************************************************************