considre this MWE:
library(magrittr)
data <- tibble::tibble(
company = c(
"Google",
"Apple"
)
) %>%
dplyr::mutate(patents = purrr::map(
.x = company,
.f = function(x) {
data <- patentsview::search_pv(
query = patentsview::qry_funs$contains(assignee_organization = x),
fields = c(
"app_number",
"app_country",
"app_date",
"patent_abstract",
"patent_date",
"patent_number",
"patent_title"
),
sort = c("app_date" = "asc"),
endpoint = "patents",
mtchd_subent_only = FALSE
)
patentsview::unnest_pv_data(data$data, pk = "patent_number") %>%
lapply(FUN = tibble::as_tibble) %>%
as.matrix() %>%
t() %>%
tibble::as_tibble()
}
)) %>%
tidyr::unnest(cols = c(patents)) %>%
tibble::column_to_rownames(var = "company") %>%
t() %>%
tibble::as_tibble(.name_repair = "unique", rownames = NA)
which results in a dataframe like this:
> data
# A tibble: 2 × 2
Google Apple
* <list> <list>
1 <tibble [25 × 5]> <tibble [25 × 5]>
2 <tibble [25 × 4]> <tibble [25 × 4]>
In this example I am using only two companies, but in the real application the number of companies is dynamic. How can I combine the nested dataframes from the different companies into one single dataframe per row?
So it looks like this:
# A tibble: 2 × 3
Google Apple Combined
* <list> <list> <list>
1 <tibble [25 × 5]> <tibble [25 × 5]> <tibble [50 × 5]>
2 <tibble [25 × 4]> <tibble [25 × 4]> <tibble [50 × 4]>
You may use rowwise
from dplyr
to combine rows in a row-wise fashion.
library(dplyr)
data %>%
rowwise() %>%
mutate(combined = list(bind_rows(c_across()))) %>%
ungroup()
# A tibble: 2 × 3
# Google Apple combined
# <list> <list> <list>
#1 <tibble [25 × 5]> <tibble [25 × 5]> <tibble [50 × 5]>
#2 <tibble [25 × 4]> <tibble [25 × 4]> <tibble [50 × 4]>