Home > OS >  How do I map over a tibble with R lang?
How do I map over a tibble with R lang?

Time:06-20

I have a tibble. Each row in my tibble contains a url and a name. I'd like to map over each row in this tibble, and pass the complete row to a function (scrape_function).

pagelist
# A tibble: 10 × 2
   href                         text            
   <chr>                        <chr>           
 1 /page-one.html               page name       
 2 /page-two.html               page 2 name
 3 /page-three.html             page 3 name       
 4 /page-four.html              page 4 name  
 5 /page-five.html              page 5 name   
... etc ...

How do I do this? I thought something like map_dfr might work, but in the code below, I'm getting an error Error: x must be a string of length 1. How do I pass the entire row to the function?

pagelist %>%
  map_dfr(function(x) { scrape_function(x) } )

CodePudding user response:

We may use map2 if the function takes two arguments i.e. the 'href' and 'text'

library(dplyr)
library(purrr)
library(tidyr)
pagelist %>%
     mutate(out = map2(href, text, ~ scrape_function(.x, .y))) %>%
     unnest(out)

If there are more than two arguments to be taken, then use pmap

pagelist %>%
    mutate(out = pmap(across(everything()), ~ scrape_function(..1, ..2, ..3)))
  • Related