Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Retrieving the length of multiple lists in a dataframe and entering the result into a new column

Given the following bibliometric data frame

papers <- structure(list(id = c("https://openalex.org/W3125056796", "https://openalex.org/W2061452228", 
"https://openalex.org/W2009485875"), display_name = c("Imagineering Otherness: Anthropological Legacies in Contemporary Tourism", 
"Migrant African women: tales of agency and belonging", "Flesh, Blood, Souls, and Households: Cultural Validity in Mortality Inquiry"
), author = list(structure(list(au_id = "https://openalex.org/A5024319069", 
    au_display_name = "Noël B. Salazar", au_orcid = "https://orcid.org/0000-0002-8346-2977", 
    author_position = "first", au_affiliation_raw = "", institution_id = NA, 
    institution_display_name = NA, institution_ror = NA, institution_country_code = NA, 
    institution_type = NA, institution_lineage = NA), class = "data.frame", row.names = c(NA, 
-1L)), structure(list(au_id = "https://openalex.org/A5022773452", 
    au_display_name = "Olga Guedes Bailey", au_orcid = NA, author_position = "first", 
    au_affiliation_raw = "", institution_id = NA, institution_display_name = NA, 
    institution_ror = NA, institution_country_code = NA, institution_type = NA, 
    institution_lineage = NA), class = "data.frame", row.names = c(NA, 
-1L)), structure(list(au_id = c("https://openalex.org/A5072236277", 
"https://openalex.org/A5015134682"), au_display_name = c("Marilyn K. Nations", 
"Mara Lucia Amaral"), au_orcid = c(NA, NA), author_position = c("first", 
"last"), au_affiliation_raw = c("Federal University of Ceará Fortaleza, Brazil", 
"PROAIS and Project HOPE Fortaleza, Brazil"), institution_id = c("https://openalex.org/I243754102", 
NA), institution_display_name = c("Universidade Federal do Ceará", 
NA), institution_ror = c("https://ror.org/03srtnf24", NA), institution_country_code = c("BR", 
NA), institution_type = c("education", NA), institution_lineage = c("https://openalex.org/I243754102", 
NA)), class = "data.frame", row.names = c(NA, -2L))), ab = c("The role of anthropology as an academic discipline that seeds tourism imaginaries across the globe is more extensive than generally acknowledged. In this article, I draw on ethnographic and archival research in Indonesia and Tanzania to examine critically the recycling of long-refuted ethnological ideas and scientific ideologies in contemporary tourism interpretation. A fine-grained analysis of local tour guide narratives and practices in two popular destinations, Yogyakarta and Arusha, illustrates empirically how outdated scholarly models, including anthropological ones, are strategically used to represent and reproduce places and peoples as authentically different and relatively static, seemingly untouched by extra-local influences.", 
"Abstract This paper explores issues of belonging and agency among asylum seekers and refugee women of African origin in the UK. It discusses the ways these women engendered resistance in their everyday life to destitution, lack of cultural recognition, and gender inequality through the foundation of their own non-governmental organization, African Women's Empowerment Forum, AWEF, a collective ‘home’ space. The focus of this account is on migrant women's agency and self-determination for the exercise of choice to be active actors in society. It points to what might be an important phenomenon on how local grassroots movements are challenging the invisibility of asylum seekers’ and refugees’ lives and expanding the notion of politics to embrace a wider notion of community politics with solidarity. AWEF is the embodiment of a social space that resonates the ‘in-between’ experience of migrant life providing stability to the women members regarding political and community identification.", 
"In the developing world the deaths of impoverished infants and young children are frequently unrecorded in vital event registries. The authors argue that cultural validity or contextual soundness grounds the meaning of mortality inquiries as much as statistical standards. Ethnographic findings from northeast Brazil are incorporated into an alternative method to survey death; this technique is compared with two standard approaches (household surveys and vital registries) and found to increase accuracy and enrich the meaning of mortality rates. Generating quality ethnographic data permits health workers to develop culturally attuned methods to calculate these rates. Techniques tailored to context, together with standardized approaches, can best portray the magnitude of human suffering and loss."
), publication_date = c("2013-01-01", "2012-05-01", "1991-09-01"
), so = c("Anthropological Quarterly", "Ethnic and Racial Studies", 
"Medical Anthropology Quarterly"), so_id = c("https://openalex.org/S127531613", 
"https://openalex.org/S96629499", "https://openalex.org/S71057445"
), host_organization = c("George Washington University", "Routledge", 
"Wiley-Blackwell"), issn_l = c("0003-5491", "0141-9870", "0745-5194"
), url = c("https://doi.org/10.1353/anq.2013.0045", "https://doi.org/10.1080/01419870.2011.628037", 
"https://doi.org/10.1525/maq.1991.5.3.02a00020"), pdf_url = c(NA_character_, 
NA_character_, NA_character_), license = c(NA_character_, NA_character_, 
NA_character_), version = c(NA_character_, NA_character_, NA_character_
), first_page = c("669", "850", "204"), last_page = c("696", 
"867", "220"), volume = c("86", "35", "5"), issue = c("3", "5", 
"3"), is_oa = c(FALSE, FALSE, FALSE), is_oa_anywhere = c(TRUE, 
TRUE, FALSE), oa_status = c("green", "green", "closed"), oa_url = c("https://lirias.kuleuven.be/bitstream/123456789/275466/2/NBS-ANQ1.pdf", 
"http://irep.ntu.ac.uk/id/eprint/10455/1/200508_6916%2520Bailey%2520Publisher.pdf", 
NA), any_repository_has_fulltext = c(TRUE, TRUE, FALSE), language = c("en", 
"en", "en"), grants = list(NA, NA, NA), cited_by_count = c(26L, 
27L, 31L), counts_by_year = list(structure(list(year = c(2023L, 
2022L, 2020L, 2019L, 2018L, 2017L, 2016L, 2014L), cited_by_count = c(2L, 
1L, 4L, 2L, 3L, 5L, 3L, 6L)), class = "data.frame", row.names = c(NA, 
-8L)), structure(list(year = c(2023L, 2022L, 2021L, 2020L, 2017L, 
2016L, 2014L, 2012L), cited_by_count = c(4L, 2L, 4L, 5L, 3L, 
4L, 3L, 2L)), class = "data.frame", row.names = c(NA, -8L)), 
    structure(list(year = c(2021L, 2019L, 2018L, 2017L, 2016L, 
    2015L, 2012L), cited_by_count = c(2L, 1L, 1L, 2L, 1L, 2L, 
    1L)), class = "data.frame", row.names = c(NA, -7L))), publication_year = c(2013L, 
2012L, 1991L), cited_by_api_url = c("https://api.openalex.org/works?filter=cites:W3125056796", 
"https://api.openalex.org/works?filter=cites:W2061452228", "https://api.openalex.org/works?filter=cites:W2009485875"
), ids = list(c(openalex = "https://openalex.org/W3125056796", 
doi = "https://doi.org/10.1353/anq.2013.0045", mag = "3125056796"
), c(openalex = "https://openalex.org/W2061452228", doi = "https://doi.org/10.1080/01419870.2011.628037", 
mag = "2061452228"), c(openalex = "https://openalex.org/W2009485875", 
doi = "https://doi.org/10.1525/maq.1991.5.3.02a00020", mag = "2009485875"
)), doi = c("https://doi.org/10.1353/anq.2013.0045", "https://doi.org/10.1080/01419870.2011.628037", 
"https://doi.org/10.1525/maq.1991.5.3.02a00020"), type = c("article", 
"article", "article"), referenced_works = list(c("https://openalex.org/W140023338", 
"https://openalex.org/W217200949", "https://openalex.org/W245765213", 
"https://openalex.org/W302956047", "https://openalex.org/W382109942", 
"https://openalex.org/W410540593", "https://openalex.org/W590172765", 
"https://openalex.org/W600868576", "https://openalex.org/W613294638", 
"https://openalex.org/W636175991", "https://openalex.org/W636500706", 
"https://openalex.org/W1510708504", "https://openalex.org/W1519749326", 
"https://openalex.org/W1522208950", "https://openalex.org/W1528671655", 
"https://openalex.org/W1537748005", "https://openalex.org/W1539932850", 
"https://openalex.org/W1570992497", "https://openalex.org/W1582218415", 
"https://openalex.org/W1586405747", "https://openalex.org/W1780911485", 
"https://openalex.org/W1974498584", "https://openalex.org/W1990878158", 
"https://openalex.org/W1991938126", "https://openalex.org/W1994035934", 
"https://openalex.org/W1998514939", "https://openalex.org/W2005691127", 
"https://openalex.org/W2008151855", "https://openalex.org/W2008730288", 
"https://openalex.org/W2023205749", "https://openalex.org/W2023234950", 
"https://openalex.org/W2026126552", "https://openalex.org/W2026423290", 
"https://openalex.org/W2033296412", "https://openalex.org/W2033645751", 
"https://openalex.org/W2037070781", "https://openalex.org/W2070983844", 
"https://openalex.org/W2077762932", "https://openalex.org/W2088993495", 
"https://openalex.org/W2091294666", "https://openalex.org/W2094250306", 
"https://openalex.org/W2099398154", "https://openalex.org/W2099809352", 
"https://openalex.org/W2102991067", "https://openalex.org/W2103040623", 
"https://openalex.org/W2113319420", "https://openalex.org/W2119017889", 
"https://openalex.org/W2142688257", "https://openalex.org/W2148498716", 
"https://openalex.org/W2149076733", "https://openalex.org/W2158877204", 
"https://openalex.org/W2171010388", "https://openalex.org/W2171507590", 
"https://openalex.org/W2316422744", "https://openalex.org/W2322897201", 
"https://openalex.org/W2520380719", "https://openalex.org/W2521108963", 
"https://openalex.org/W2602592981", "https://openalex.org/W2798836521", 
"https://openalex.org/W2800793723", "https://openalex.org/W3000076761", 
"https://openalex.org/W3097135668", "https://openalex.org/W3122396436", 
"https://openalex.org/W3123568031"), c("https://openalex.org/W214069973", 
"https://openalex.org/W1966114252", "https://openalex.org/W1999681598", 
"https://openalex.org/W2013879700", "https://openalex.org/W2042427709", 
"https://openalex.org/W2090733135", "https://openalex.org/W2141011183", 
"https://openalex.org/W2151579054", "https://openalex.org/W2497030174", 
"https://openalex.org/W4206618199", "https://openalex.org/W4211063581", 
"https://openalex.org/W4233024576", "https://openalex.org/W4239419073", 
"https://openalex.org/W4302569202"), c("https://openalex.org/W2000910032", 
"https://openalex.org/W2013754886", "https://openalex.org/W2019391768", 
"https://openalex.org/W2038478456", "https://openalex.org/W2039727337", 
"https://openalex.org/W2087775509", "https://openalex.org/W2159615857", 
"https://openalex.org/W2506283631", "https://openalex.org/W2552970619", 
"https://openalex.org/W3136381751", "https://openalex.org/W4239358932", 
"https://openalex.org/W4239860347", "https://openalex.org/W4253820265"
)), related_works = list(c("https://openalex.org/W2140008790", 
"https://openalex.org/W3099695869", "https://openalex.org/W2171036068", 
"https://openalex.org/W2064836460", "https://openalex.org/W2748952813", 
"https://openalex.org/W453495043", "https://openalex.org/W2102040893", 
"https://openalex.org/W4239456681", "https://openalex.org/W4315698954", 
"https://openalex.org/W4210526204"), c("https://openalex.org/W149733664", 
"https://openalex.org/W2017080553", "https://openalex.org/W2079784576", 
"https://openalex.org/W2174870299", "https://openalex.org/W2989650672", 
"https://openalex.org/W3046599242", "https://openalex.org/W3114024476", 
"https://openalex.org/W3179630410", "https://openalex.org/W3194561998", 
"https://openalex.org/W4244358585"), c("https://openalex.org/W2378387552", 
"https://openalex.org/W2365600428", "https://openalex.org/W2389257519", 
"https://openalex.org/W2374219405", "https://openalex.org/W2748952813", 
"https://openalex.org/W4302559785", "https://openalex.org/W2000943029", 
"https://openalex.org/W2950376466", "https://openalex.org/W1969483203", 
"https://openalex.org/W2466303427")), is_paratext = c(FALSE, 
FALSE, FALSE), is_retracted = c(FALSE, FALSE, FALSE), concepts = list(
    structure(list(id = c("https://openalex.org/C18918823", "https://openalex.org/C179454799", 
    "https://openalex.org/C2775899829", "https://openalex.org/C2779357621", 
    "https://openalex.org/C144024400", "https://openalex.org/C527412718", 
    "https://openalex.org/C158071213", "https://openalex.org/C19165224", 
    "https://openalex.org/C199033989", "https://openalex.org/C36289849", 
    "https://openalex.org/C29595303", "https://openalex.org/C205649164", 
    "https://openalex.org/C17744445", "https://openalex.org/C2549261", 
    "https://openalex.org/C166957645", "https://openalex.org/C199539241", 
    "https://openalex.org/C71924100", "https://openalex.org/C41895202", 
    "https://openalex.org/C138885662", "https://openalex.org/C94625758", 
    "https://openalex.org/C41008148", "https://openalex.org/C118487528", 
    "https://openalex.org/C199360897"), wikidata = c("https://www.wikidata.org/wiki/Q49389", 
    "https://www.wikidata.org/wiki/Q132151", "https://www.wikidata.org/wiki/Q3109007", 
    "https://www.wikidata.org/wiki/Q655495", "https://www.wikidata.org/wiki/Q21201", 
    "https://www.wikidata.org/wiki/Q855395", "https://www.wikidata.org/wiki/Q7257", 
    "https://www.wikidata.org/wiki/Q23404", "https://www.wikidata.org/wiki/Q1318295", 
    "https://www.wikidata.org/wiki/Q34749", "https://www.wikidata.org/wiki/Q165650", 
    "https://www.wikidata.org/wiki/Q1071", "https://www.wikidata.org/wiki/Q36442", 
    "https://www.wikidata.org/wiki/Q43455", "https://www.wikidata.org/wiki/Q23498", 
    "https://www.wikidata.org/wiki/Q7748", "https://www.wikidata.org/wiki/Q11190", 
    "https://www.wikidata.org/wiki/Q8162", "https://www.wikidata.org/wiki/Q5891", 
    "https://www.wikidata.org/wiki/Q7163", "https://www.wikidata.org/wiki/Q21198", 
    "https://www.wikidata.org/wiki/Q161437", "https://www.wikidata.org/wiki/Q9143"
    ), display_name = c("Tourism", "Ethnography", "Globe", "Tanzania", 
    "Sociology", "Interpretation (philosophy)", "Ideology", "Anthropology", 
    "Narrative", "Social science", "Media studies", "Geography", 
    "Political science", "Ethnology", "Archaeology", "Law", "Medicine", 
    "Linguistics", "Philosophy", "Politics", "Computer science", 
    "Ophthalmology", "Programming language"), level = c(2L, 2L, 
    2L, 2L, 0L, 2L, 3L, 1L, 2L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 
    1L, 0L, 2L, 0L, 1L, 1L), score = c(0.7789825, 0.7643638, 
    0.75725675, 0.6268818, 0.6117397, 0.60966444, 0.5802863, 
    0.47963363, 0.43749186, 0.4081406, 0.35234696, 0.24193704, 
    0.19674754, 0.19646946, 0.11082253, 0.06364152, 0, 0, 0, 
    0, 0, 0, 0)), class = "data.frame", row.names = c(NA, -23L
    )), structure(list(id = c("https://openalex.org/C2781188222", 
    "https://openalex.org/C173145845", "https://openalex.org/C108170787", 
    "https://openalex.org/C2780641677", "https://openalex.org/C107993555", 
    "https://openalex.org/C20555606", "https://openalex.org/C94625758", 
    "https://openalex.org/C144024400", "https://openalex.org/C50962388", 
    "https://openalex.org/C17744445", "https://openalex.org/C199539241", 
    "https://openalex.org/C36289849", "https://openalex.org/C121332964", 
    "https://openalex.org/C120665830"), wikidata = c("https://www.wikidata.org/wiki/Q929651", 
    "https://www.wikidata.org/wiki/Q131572", "https://www.wikidata.org/wiki/Q3951828", 
    "https://www.wikidata.org/wiki/Q815726", "https://www.wikidata.org/wiki/Q1662673", 
    "https://www.wikidata.org/wiki/Q868575", "https://www.wikidata.org/wiki/Q7163", 
    "https://www.wikidata.org/wiki/Q21201", "https://www.wikidata.org/wiki/Q762018", 
    "https://www.wikidata.org/wiki/Q36442", "https://www.wikidata.org/wiki/Q7748", 
    "https://www.wikidata.org/wiki/Q34749", "https://www.wikidata.org/wiki/Q413", 
    "https://www.wikidata.org/wiki/Q14620"), display_name = c("Grassroots", 
    "Refugee", "Agency (philosophy)", "Solidarity", "Gender studies", 
    "Empowerment", "Politics", "Sociology", "Invisibility", "Political science", 
    "Law", "Social science", "Physics", "Optics"), level = c(3L, 
    2L, 2L, 3L, 1L, 2L, 2L, 0L, 2L, 0L, 1L, 1L, 0L, 1L), score = c(0.82434046, 
    0.7657643, 0.7383462, 0.71981716, 0.6658181, 0.64661604, 
    0.6213448, 0.610957, 0.5197339, 0.3979023, 0.18783087, 0.1530168, 
    0, 0)), class = "data.frame", row.names = c(NA, -14L)), structure(list(
        id = c("https://openalex.org/C179454799", "https://openalex.org/C2780876879", 
        "https://openalex.org/C39920170", "https://openalex.org/C2779343474", 
        "https://openalex.org/C15744967", "https://openalex.org/C144024400", 
        "https://openalex.org/C19165224", "https://openalex.org/C205649164", 
        "https://openalex.org/C41008148", "https://openalex.org/C166957645", 
        "https://openalex.org/C542102704", "https://openalex.org/C199360897"
        ), wikidata = c("https://www.wikidata.org/wiki/Q132151", 
        "https://www.wikidata.org/wiki/Q3054749", "https://www.wikidata.org/wiki/Q693083", 
        "https://www.wikidata.org/wiki/Q3109175", "https://www.wikidata.org/wiki/Q9418", 
        "https://www.wikidata.org/wiki/Q21201", "https://www.wikidata.org/wiki/Q23404", 
        "https://www.wikidata.org/wiki/Q1071", "https://www.wikidata.org/wiki/Q21198", 
        "https://www.wikidata.org/wiki/Q23498", "https://www.wikidata.org/wiki/Q183257", 
        "https://www.wikidata.org/wiki/Q9143"), display_name = c("Ethnography", 
        "Meaning (existential)", "Soundness", "Context (archaeology)", 
        "Psychology", "Sociology", "Anthropology", "Geography", 
        "Computer science", "Archaeology", "Psychotherapist", 
        "Programming language"), level = c(2L, 2L, 2L, 2L, 0L, 
        0L, 1L, 0L, 0L, 1L, 1L, 1L), score = c(0.7762847, 0.712257, 
        0.5775342, 0.5595404, 0.390778, 0.3559336, 0.23764312, 
        0.23022234, 0.073655695, 0, 0, 0)), class = "data.frame", row.names = c(NA, 
    -12L))), ethnographic = c(1, 0, 1), evolutionary = c(0, 0, 
0), quantitative = c(0, 0, 0), authorcount2 = list(NA_integer_, 
    NA_integer_, NA_integer_), authorcount = list(1L, 1L, 2L)), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))

I’m trying to count the length of each list in the author column and enter the resulting number into a new column, authorcount.

I have tried:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

papers$authorcount <- Map(nrow,papers$author)

The problem is that the result of this is a list object. I want column authorcount to be numeric, a simple integer.

>Solution :

Using vapply you could do:

papers$authorcount = vapply(papers$author, nrow, FUN.VALUE = integer(1))

papers$authorcount
#> [1] 1 1 2

EDIT Here is an approach to account for non data frame elements using some simplified fake example data:

set.seed(123)

papers <- tibble::tibble(
  author = c(
    replicate(5, data.frame(a = seq_len(sample(1:5, 1))), simplify = FALSE),
    NA
  )
)
papers
#> # A tibble: 6 × 1
#>   author      
#>   <list>      
#> 1 <df [3 × 1]>
#> 2 <df [3 × 1]>
#> 3 <df [2 × 1]>
#> 4 <df [2 × 1]>
#> 5 <df [3 × 1]>
#> 6 <lgl [1]>

papers$authorcount <- vapply(
  papers$author, \(x) if (is.data.frame(x)) nrow(x) else NA_integer_,
  FUN.VALUE = integer(1)
)

papers$authorcount
#> [1]  3  3  2  2  3 NA
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading