Duck
Duck

Reputation: 39595

Extracting and unnesting list objects from a dataframe as new variables

I am working with a very special dataframe in R that has some variables defined as lists. My dataframe df is included at the end of the post with dput(). The issue with this dataframe is that holds lists as variables:

df$position_groups.profile_positions.1.date.start.year
[[1]]
[1] 2019 2017

df$position_groups.profile_positions.1.date.start.month
[[1]]
[1]  2 10

Those are examples. In the dataframe there are plenty of lists. I would like to extract the content from those lists in the dataframe as new variables inside same dataframe so that we can have something like this:

position_groups.profile_positions.1.date.start.year1
2019
position_groups.profile_positions.1.date.start.year2
2017

And so on for all the values inside all the lists.

This is my dataframe:

#Data
df <- structure(list(position_groups.profile_positions.1.date.start.year = list(
    c(2019L, 2017L)), position_groups.profile_positions.1.date.start.month = list(
    c(2L, 10L)), position_groups.profile_positions.1.title = list(
    c("Sub Gerente de Franquicias y Servicios complementarios ", 
    "Sub gerente de Franquicias ")), position_groups.profile_positions.1.location = list(
    c(NA, NA)), position_groups.profile_positions.1.company = list(
    c("Corporación GPF (Grupo Fybeca)", "Corporación GPF (Grupo Fybeca)"
    )), position_groups.profile_positions.2.date.end.year = 2017L, 
    position_groups.profile_positions.2.date.end.month = 4L, 
    position_groups.profile_positions.2.date.start.year = 2016L, 
    position_groups.profile_positions.2.date.start.month = 11L, 
    position_groups.profile_positions.2.title = "DIRECTOR COMERCIAL", 
    position_groups.profile_positions.2.location = "QUITO", position_groups.profile_positions.2.company = "ECASA", 
    position_groups.profile_positions.3.date.end.year = list(
        c(2016L, 2013L, 2011L)), position_groups.profile_positions.3.date.end.month = list(
        c(6L, 12L, 12L)), position_groups.profile_positions.3.date.start.year = list(
        c(2014L, 2012L, 2011L)), position_groups.profile_positions.3.date.start.month = list(
        c(1L, 1L, 4L)), position_groups.profile_positions.3.title = list(
        c("Gerente nacional de ventas", "Gerente de cuentas", 
        "Supervisor nacional de trade Marketing ")), position_groups.profile_positions.3.location = list(
        c("Quito", NA, "Quito")), position_groups.profile_positions.3.company = list(
        c("Electrolux", "Electrolux", "Electrolux")), position_groups.profile_positions.4.date.end.year = 2011L, 
    position_groups.profile_positions.4.date.end.month = 4L, 
    position_groups.profile_positions.4.date.start.year = 2009L, 
    position_groups.profile_positions.4.date.start.month = 4L, 
    position_groups.profile_positions.4.title = "Coordinador de Trade Marketing ", 
    position_groups.profile_positions.4.location = "Quito", position_groups.profile_positions.4.company = "Tesalia Springs Co", 
    position_groups.profile_positions.5.date.end.year = 2008L, 
    position_groups.profile_positions.5.date.end.month = 5L, 
    position_groups.profile_positions.5.date.start.year = 2005L, 
    position_groups.profile_positions.5.date.start.month = 1L, 
    position_groups.profile_positions.5.title = "Asesor comercial ", 
    position_groups.profile_positions.5.location = "Quito", position_groups.profile_positions.5.company = "Samsung", 
    position_groups.date.start.year = list(c(2017L, 2016L, 2011L, 
    2009L, 2005L)), position_groups.date.start.month = list(c(10L, 
    11L, 4L, 4L, 1L)), position_groups.date.end.year = list(c(NA, 
    2017L, 2016L, 2011L, 2008L)), position_groups.date.end.month = list(
        c(NA, 4L, 6L, 4L, 5L)), position_groups.company.logo = list(
        c("https://media-exp1.licdn.com/dms/image/C4E0BAQHZW0KT7gtzpg/company-logo_400_400/0/1634838463806?e=1645660800&v=beta&t=maKQnWa0Ry0oP9A9EB9_rM1xUyl9K-G1mVk0zP4vWg8", 
        NA, "https://media-exp1.licdn.com/dms/image/C4E0BAQEZiaaUvM-yOg/company-logo_400_400/0/1521536555594?e=1645660800&v=beta&t=v--wQBi-LJufz3md5WhfqPXgu9ouBl3st2318sZNVgU", 
        NA, NA)), position_groups.company.name = list(c("Corporación GPF (Grupo Fybeca)", 
    "ECASA", "Electrolux", "Tesalia Springs Co", "Samsung")), 
    position_groups.company.url = list(c("https://www.linkedin.com/company/corporaci%C3%B3n-gpf/", 
    NA, "https://www.linkedin.com/company/electrolux/", "https://www.linkedin.com/company/tesalia-springs-co/", 
    "https://www.linkedin.com/company/samsung/")), position_groups.company.employees.end = list(
        c(5000L, NA, NA, NA, NA)), position_groups.company.employees.start = list(
        c(1001L, NA, 10001L, NA, 10001L)), profile_id = "pablo-espinosa-zea-5568b650", 
    certifications.company.logo = list(c("https://media-exp1.licdn.com/dms/image/C4D0BAQGZKGGJ75S72g/company-logo_400_400/0/1629754938156?e=1645660800&v=beta&t=7CUks6TACkoAEgW6YPM__W8EAm-kzrNUgjtHT43DrEw", 
    "https://media-exp1.licdn.com/dms/image/C4D0BAQGZKGGJ75S72g/company-logo_400_400/0/1629754938156?e=1645660800&v=beta&t=7CUks6TACkoAEgW6YPM__W8EAm-kzrNUgjtHT43DrEw", 
    "https://media-exp1.licdn.com/dms/image/C4D0BAQGZKGGJ75S72g/company-logo_400_400/0/1629754938156?e=1645660800&v=beta&t=7CUks6TACkoAEgW6YPM__W8EAm-kzrNUgjtHT43DrEw", 
    "https://media-exp1.licdn.com/dms/image/C4D0BAQGZKGGJ75S72g/company-logo_400_400/0/1629754938156?e=1645660800&v=beta&t=7CUks6TACkoAEgW6YPM__W8EAm-kzrNUgjtHT43DrEw", 
    "https://media-exp1.licdn.com/dms/image/C4D0BAQGZKGGJ75S72g/company-logo_400_400/0/1629754938156?e=1645660800&v=beta&t=7CUks6TACkoAEgW6YPM__W8EAm-kzrNUgjtHT43DrEw"
    )), certifications.company.name = list(c("LinkedIn", "LinkedIn", 
    "LinkedIn", "LinkedIn", "LinkedIn")), certifications.date.start.year = list(
        c(2021L, 2021L, 2021L, 2021L, 2021L)), certifications.date.start.month = list(
        c(10L, 10L, 10L, 10L, 10L)), certifications.license_number = list(
        c(NA, NA, NA, NA, NA)), certifications.url = list(c("https://www.linkedin.com/learning/como-desarrollar-la-inteligencia-emocional?trk=flagship-lil_details_certification", 
    "https://www.linkedin.com/learning/habilidades-de-coaching-para-lideres-y-gerentes?trk=flagship-lil_details_certification", 
    "https://www.linkedin.com/learning/pensamiento-estrategico?trk=flagship-lil_details_certification", 
    "https://www.linkedin.com/learning/retail-customer-service-management-and-coaching?trk=flagship-lil_details_certification", 
    "https://www.linkedin.com/learning/the-secret-to-better-decisions-stop-hoarding-chips?trk=flagship-lil_details_certification"
    )), certifications.name = list(c("Cómo desarrollar la inteligencia emocional", 
    "Habilidades de coaching para líderes y gerentes", "Pensamiento estratégico", 
    "Retail Customer Service Management and Coaching", "The Secret to Better Decisions: Stop Hoarding Chips"
    )), certifications.display_source = list(c("linkedin.com", 
    "linkedin.com", "linkedin.com", "linkedin.com", "linkedin.com"
    )), certifications.authority = list(c("LinkedIn", "LinkedIn", 
    "LinkedIn", "LinkedIn", "LinkedIn")), first_name = "Pablo", 
    influencer = FALSE, profile_picture = NA, last_name = "Espinosa Zea", 
    education.field_of_study = list(c(NA, "Ventas generales, promoción comercial y actividades de marketing afines"
    )), education.degree_name = list(c("Master en  programaciòn Neurolinguistica y Neuro Marketing ", 
    "Ingeniero en Negocios Internacionales")), education.date.end.year = list(
        c(NA, 2013L)), education.date.start.year = list(c(NA, 
    2010L)), education.school.logo = list(c(NA, NA)), education.school.name = list(
        c("NDHL", "Universidad Alfredo Pérez Guerrero")), skills = list(
        c("Administración y dirección de empresas", "Servicio de atención al cliente", 
        "Microsoft Office", "Gestión de proyectos", "Estrategia de mercadotecnia", 
        "Gerencia de ventas", "Negociación", "Liderazgo de equipos multidisciplinarios", 
        "Estrategia empresarial", "Liderazgo de equipos", "Estrategia", 
        "Desarrollo del personal", "Orientación para el desarrollo de carrera profesional", 
        "Reflexión estratégica", "Toma de decisiones", "Inteligencia emocional", 
        "Coaching", "Gestión")), premium = FALSE, birth_date = NA, 
    industry = "Retail", summary = NA, location.country = NA, 
    location.short = "Ecuador", location.default = "Ecuador", 
    languages.primary_locale.country = "ES", languages.primary_locale.language = "es", 
    languages.supported_locales.country = "ES", languages.supported_locales.language = "es"), row.names = 1L, class = "data.frame")

Many thanks.

Upvotes: 0

Views: 37

Answers (1)

jblood94
jblood94

Reputation: 16981

This seems to be basically what you're asking for:

data.frame(lapply(unlist(df, recursive = FALSE), as.list))

The names seem to get rather long, though, as they contain the individual values.

Upvotes: 1

Related Questions