简体   繁体   中英

trying to apply the function plyr::ldply on lists to convert into a data.frame of differing number of rows

I am trying to apply the ldply function from the plyr package to convert lists to a data frame but I keep running into an error. I apply the following line:

final <- plyr::ldply(data, data.frame)

Which returns this error:

Error:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 1, 0

Does the lists have to have the same number of rows? I thought since I am trying to bind the rows then only the columns would need to be equal in each list.

Data:

list(`0000001750` = list(information = structure(list(name = "AAR CORP", 
    cik = "0000001750", fiscal_year_end = "0531", company_href = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=0000001750&owner=exclude&count=100", 
    sic = "3720", sic_description = "AIRCRAFT &amp; PARTS", state_location = "IL", 
    state_incorporation = "DE", mailing_city = "WOOD DALE", mailing_state = "IL", 
    mailing_zip = "60191", mailing_street = "1100 N WOOD DALE RD", 
    mailing_street2 = NA_character_, business_city = "WOOD DALE", 
    business_state = "IL", business_zip = "60191", business_street = "1100 N WOOD DALE RD", 
    business_street2 = NA_character_, business_phone = "6302272000"), row.names = c(NA, 
-1L), class = "data.frame"), filings = structure(list(accession_number = c("0001047469-18-004978", 
"0001047469-17-004528", "0001047469-16-014299", "0001047469-15-006136", 
"0001047469-14-006243", "0001047469-13-007797", "0001047469-12-007300", 
"0001047469-11-006302", "0001047469-10-006500", "0001047469-09-006783", 
"0001047469-08-008126", "0001104659-07-055173", "0001104659-06-047248", 
"0001104659-05-033688", "0001047469-04-023905", "0001047469-03-028005", 
"0000912057-02-033450", "0000912057-01-530303", "0000912057-00-039006", 
"0001047469-99-032978", "0001047469-98-032283", "0000912057-97-028915", 
"0000912057-96-018355", "0000912057-95-006316", "0000912057-94-002818"
), act = c("34", "34", "34", "34", "34", "34", "34", "34", "34", 
"34", "34", "34", "34", "34", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), file_number = c("001-06263", "001-06263", "001-06263", 
"001-06263", "001-06263", "001-06263", "001-06263", "001-06263", 
"001-06263", "001-06263", "001-06263", "001-06263", "001-06263", 
"001-06263", "001-06263", "001-06263", "001-06263", "001-06263", 
"001-06263", "001-06263", "001-06263", "001-06263", "001-06263", 
"001-06263", "001-06263"), filing_date = structure(c(1531260000, 
1499810400, 1468360800, 1436911200, 1405548000, 1374789600, 1342648800, 
1310508000, 1279231200, 1247695200, 1215727200, 1184882400, 1153087200, 
1121983200, 1090447200, 1061157600, 1030312800, 998863200, 967068000, 
934927200, 903564000, 872200800, 840492000, 808092000, 777679200
), class = c("POSIXct", "POSIXt"), tzone = ""), accepted_date = structure(c(1531260000, 
1499810400, 1468360800, 1436911200, 1405548000, 1374703200, 1342648800, 
1310508000, 1279231200, 1247695200, 1215727200, 1184882400, 1153087200, 
1121983200, 1090447200, 1061157600, 1030312800, 998863200, 967068000, 
934927200, 903564000, 872200800, 840492000, 808092000, 777679200
), class = c("POSIXct", "POSIXt"), tzone = ""), href = c("https://www.sec.gov/Archives/edgar/data/1750/000104746918004978/0001047469-18-004978-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746917004528/0001047469-17-004528-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746916014299/0001047469-16-014299-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746915006136/0001047469-15-006136-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746914006243/0001047469-14-006243-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746913007797/0001047469-13-007797-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746912007300/0001047469-12-007300-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746911006302/0001047469-11-006302-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746910006500/0001047469-10-006500-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746909006783/0001047469-09-006783-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746908008126/0001047469-08-008126-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000110465907055173/0001104659-07-055173-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000110465906047248/0001104659-06-047248-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000110465905033688/0001104659-05-033688-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746904023905/0001047469-04-023905-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746903028005/0001047469-03-028005-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000091205702033450/0000912057-02-033450-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000091205701530303/0000912057-01-530303-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000091205700039006/0000912057-00-039006-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/0001047469-99-032978-index.html", 
"https://www.sec.gov/Archives/edgar/data/1750/0001047469-98-032283-index.html", 
"https://www.sec.gov/Archives/edgar/data/1750/0000912057-97-028915-index.html", 
"https://www.sec.gov/Archives/edgar/data/1750/0000912057-96-018355-index.html", 
"https://www.sec.gov/Archives/edgar/data/1750/0000912057-95-006316-index.html", 
"https://www.sec.gov/Archives/edgar/data/1750/0000912057-94-002818-index.html"
), type = c("10-K", "10-K", "10-K", "10-K", "10-K", "10-K", "10-K", 
"10-K", "10-K", "10-K", "10-K", "10-K", "10-K", "10-K", "10-K", 
"10-K", "10-K", "10-K405", "10-K405", "10-K405", "10-K405", "10-K405", 
"10-K405", "10-K", "10-K"), film_number = c("18949105", "17961746", 
"161765559", "15989710", "14980278", "13987679", "12970392", 
"11966075", "10956500", "09948386", "08949571", "07991544", "06964375", 
"05969331", "04926022", "03852100", "02747889", "1723694", "709276", 
"99695615", "98694938", "97668667", "96617988", "95561522", "94545798"
), form_name = c("Annual report [Section 13 and 15(d), not S-K Item 405]", 
"Annual report [Section 13 and 15(d), not S-K Item 405]", "Annual report [Section 13 and 15(d), not S-K Item 405]", 
"Annual report [Section 13 and 15(d), not S-K Item 405]", "Annual report [Section 13 and 15(d), not S-K Item 405]", 
"Annual report [Section 13 and 15(d), not S-K Item 405]", "Annual report [Section 13 and 15(d), not S-K Item 405]", 
"Annual report [Section 13 and 15(d), not S-K Item 405]", "Annual report [Section 13 and 15(d), not S-K Item 405]", 
"Annual report [Section 13 and 15(d), not S-K Item 405]", "Annual report [Section 13 and 15(d), not S-K Item 405]", 
"Annual report [Section 13 and 15(d), not S-K Item 405]", "Annual report [Section 13 and 15(d), not S-K Item 405]", 
"Annual report [Section 13 and 15(d), not S-K Item 405]", "Annual report [Section 13 and 15(d), not S-K Item 405]", 
"Annual report [Section 13 and 15(d), not S-K Item 405]", "Annual report [Section 13 and 15(d), not S-K Item 405]", 
"Annual report [Sections 13 and 15(d), S-K Item 405]", "Annual report [Sections 13 and 15(d), S-K Item 405]", 
"Annual report [Sections 13 and 15(d), S-K Item 405]", "Annual report [Sections 13 and 15(d), S-K Item 405]", 
"Annual report [Sections 13 and 15(d), S-K Item 405]", "Annual report [Sections 13 and 15(d), S-K Item 405]", 
"Annual report [Section 13 and 15(d), not S-K Item 405]", "Annual report [Section 13 and 15(d), not S-K Item 405]"
), description = c(NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_), size = c("15 MB", "15 MB", "16 MB", 
"17 MB", "16 MB", "21 MB", "14 MB", "5 MB", "1 MB", "1 MB", "1 MB", 
"2 MB", "2 MB", "3 MB", "808 KB", "2 MB", "1 MB", "985 KB", "452 KB", 
"274 KB", "173 KB", "153 KB", "183 KB", "306 KB", "213 KB")), row.names = c(NA, 
-25L), class = "data.frame")), `0000313368` = list(information = structure(list(
    name = "ABS INDUSTRIES INC /DE/", cik = "0000313368", fiscal_year_end = "1031", 
    company_href = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=0000313368&owner=exclude&count=100", 
    sic = "3460", sic_description = "METAL FORGING &amp; STAMPINGS", 
    state_location = "OH", state_incorporation = "OH", mailing_city = "WILLOUGHBY", 
    mailing_state = "OH", mailing_zip = "44094", mailing_street = "P O BOX 630", 
    mailing_street2 = "4230 INTERSTATE SQ I SUITE 300", business_city = "WILLOUGHBY", 
    business_state = "OH", business_zip = "44094", business_street = "P O BOX 630", 
    business_street2 = "4230 INTERSTATE SQ I SUITE 300", business_phone = "2169462274"), row.names = c(NA, 
-1L), class = "data.frame"), filings = structure(list(accession_number = "0000902873-96-000014", 
    act = NA_character_, file_number = "000-09556", filing_date = structure(822956400, class = c("POSIXct", 
    "POSIXt"), tzone = ""), accepted_date = structure(822956400, class = c("POSIXct", 
    "POSIXt"), tzone = ""), href = "https://www.sec.gov/Archives/edgar/data/313368/0000902873-96-000014-index.html", 
    type = "10-K", film_number = "96508681", form_name = "Annual report [Section 13 and 15(d), not S-K Item 405]", 
    description = NA_character_, size = "12 KB"), row.names = c(NA, 
-1L), class = "data.frame")), `0000910627` = list(information = structure(list(
    name = "ACF INDUSTRIES HOLDING CORP", cik = "0000910627", 
    fiscal_year_end = "1231", company_href = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=0000910627&owner=exclude&count=100", 
    sic = "3743", sic_description = "RAILROAD EQUIPMENT", state_location = "NY", 
    state_incorporation = "DE", mailing_city = "WHITE PLAINS", 
    mailing_state = "NY", mailing_zip = "10601", mailing_street = "445 HAMILTON AVENUE", 
    mailing_street2 = "SUITE 1210", business_city = "WHITE PLAINS", 
    business_state = "NY", business_zip = "10601", business_street = "445 HAMILTON AVENUE", 
    business_street2 = "SUITE 1210", business_phone = "914-614-7000"), row.names = c(NA, 
-1L), class = "data.frame"), filings = structure(list(), .Names = character(0), class = "data.frame", row.names = integer(0))), 
    `0000702511` = list(information = structure(list(name = "ACS ENTERPRISES INC", 
        cik = "0000702511", fiscal_year_end = "1231", company_href = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=0000702511&owner=exclude&count=100", 
        sic = "4841", sic_description = "CABLE &amp; OTHER PAY TELEVISION SERVICES", 
        state_location = "PA", state_incorporation = "PA", mailing_city = NA_character_, 
        mailing_state = NA_character_, mailing_zip = NA_character_, 
        mailing_street = NA_character_, mailing_street2 = NA_character_, 
        business_city = "TREVOSE", business_state = "PA", business_zip = "19053-6789", 
        business_street = "2510 METROPOLITAN DR", business_street2 = NA_character_, 
        business_phone = "2153969400"), row.names = c(NA, -1L
    ), class = "data.frame"), filings = structure(list(), .Names = character(0), class = "data.frame", row.names = integer(0))), 
    `0000061478` = list(information = structure(list(name = "ADC TELECOMMUNICATIONS INC", 
        cik = "0000061478", fiscal_year_end = "1031", company_href = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=0000061478&owner=exclude&count=100", 
        sic = "3661", sic_description = "TELEPHONE &amp; TELEGRAPH APPARATUS", 
        state_location = "MN", state_incorporation = "MN", mailing_city = "EDEN PRAIRIE", 
        mailing_state = "MN", mailing_zip = "55344", mailing_street = "13625 TECHNOLOGY DRIVE", 
        mailing_street2 = NA_character_, business_city = "EDEN PRAIRIE", 
        business_state = "MN", business_zip = "55344", business_street = "13625 TECHNOLOGY DRIVE", 
        business_street2 = NA_character_, business_phone = "9529388080"), row.names = c(NA, 
    -1L), class = "data.frame"), filings = structure(list(accession_number = c("0000950123-10-108231", 
    "0000950123-09-064850", "0000950152-08-010514", "0000950137-07-018659", 
    "0000950137-07-003714", "0000950137-07-000193", "0000950134-06-000594", 
    "0001206774-05-000032", "0001206774-04-000013", "0001047469-03-001075", 
    "0000912057-02-001614", "0000912057-01-523816", "0000912057-01-000926", 
    "0000912057-99-011120", "0001047469-98-045697", "0001047469-98-001384", 
    "0000912057-97-000580", "0000061478-96-000002", "0000912057-94-004277", 
    "0000912057-94-000091", "0000912057-94-000081"), act = c("34", 
    "34", "34", "34", "34", "34", "34", "34", NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA), file_number = c("000-01424", 
    "000-01424", "000-01424", "000-01424", "000-01424", "000-01424", 
    "000-01424", "000-01424", "000-01424", "000-01424", "000-01424", 
    "000-01424", "000-01424", "000-01424", "000-01424", "000-01424", 
    "000-01424", "000-01424", "000-01424", "000-01424", "000-01424"
    ), filing_date = structure(c(1290466800, 1258930800, 1229900400, 
    1197932400, 1173740400, 1168297200, 1137452400, 1105657200, 
    1074121200, 1042153200, 1011135600, 994975200, 979081200, 
    946508400, 915058800, 884905200, 852850800, 820796400, 788050800, 
    758415600, 758329200), class = c("POSIXct", "POSIXt"), tzone = ""), 
        accepted_date = structure(c(1290466800, 1258671600, 1229641200, 
        1197932400, 1173740400, 1168297200, 1137452400, 1105657200, 
        1074121200, 1042153200, 1011135600, 994975200, 979081200, 
        946508400, 915058800, 884905200, 852850800, 820796400, 
        788050800, 758415600, 758329200), class = c("POSIXct", 
        "POSIXt"), tzone = ""), href = c("https://www.sec.gov/Archives/edgar/data/61478/000095012310108231/0000950123-10-108231-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000095012309064850/0000950123-09-064850-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000095015208010514/0000950152-08-010514-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000095013707018659/0000950137-07-018659-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000095013707003714/0000950137-07-003714-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000095013707000193/0000950137-07-000193-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000095013406000594/0000950134-06-000594-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000120677405000032/0001206774-05-000032-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000120677404000013/0001206774-04-000013-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000104746903001075/0001047469-03-001075-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000091205702001614/0000912057-02-001614-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000091205701523816/0000912057-01-523816-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/000091205701000926/0000912057-01-000926-index.htm", 
        "https://www.sec.gov/Archives/edgar/data/61478/0000912057-99-011120-index.html", 
        "https://www.sec.gov/Archives/edgar/data/61478/0001047469-98-045697-index.html", 
        "https://www.sec.gov/Archives/edgar/data/61478/0001047469-98-001384-index.html", 
        "https://www.sec.gov/Archives/edgar/data/61478/0000912057-97-000580-index.html", 
        "https://www.sec.gov/Archives/edgar/data/61478/0000061478-96-000002-index.html", 
        "https://www.sec.gov/Archives/edgar/data/61478/0000912057-94-004277-index.html", 
        "https://www.sec.gov/Archives/edgar/data/61478/0000912057-94-000091-index.html", 
        "https://www.sec.gov/Archives/edgar/data/61478/0000912057-94-000081-index.html"
        ), type = c("10-K", "10-KT", "10-K", "10-K", "10-K/A", 
        "10-K", "10-K", "10-K", "10-K", "10-K", "10-K", "10-K405/A", 
        "10-K405", "10-K", "10-K", "10-K", "10-K", "10-K", "10-K", 
        "10-K/A", "10-K"), film_number = c("101211851", "091199947", 
        "081262274", "071313162", "07690805", "07520905", "06531830", 
        "05530480", "04527767", "03511201", "2511031", "1681275", 
        "1506050", "99784560", "98779883", "98508756", "97503579", 
        "96501260", "94565833", "94501411", "94501101"), form_name = c("Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Transition reports [Rule 13a-10 or 15d-10]", "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Sections 13 and 15(d), S-K Item 405]", 
        "Annual report [Sections 13 and 15(d), S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]"
        ), description = c(NA_character_, NA_character_, NA_character_, 
        NA_character_, NA_character_, NA_character_, NA_character_, 
        NA_character_, NA_character_, NA_character_, NA_character_, 
        NA_character_, NA_character_, NA_character_, NA_character_, 
        NA_character_, NA_character_, NA_character_, NA_character_, 
        NA_character_, NA_character_), size = c("1 MB", "1 MB", 
        "1 MB", "1 MB", "2 MB", "2 MB", "1 MB", "1 MB", "1 MB", 
        "1 MB", "2 MB", "570 KB", "790 KB", "1 MB", "571 KB", 
        "303 KB", "547 KB", "166 KB", "177 KB", "90 KB", "764 KB"
        )), row.names = c(NA, -21L), class = "data.frame")), 
    `0000004911` = list(information = structure(list(name = "AEL INDUSTRIES INC", 
        cik = "0000004911", fiscal_year_end = "0226", company_href = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=0000004911&owner=exclude&count=100", 
        sic = "3812", sic_description = "SEARCH, DETECTION, NAVIGATION, GUIDANCE, AERONAUTICAL SYS", 
        state_location = "PA", state_incorporation = "PA", mailing_city = "LANSDALE", 
        mailing_state = "PA", mailing_zip = "19446", mailing_street = "305 RICHARDSON ROAD", 
        mailing_street2 = NA_character_, business_city = "LANSDALE", 
        business_state = "PA", business_zip = "19446", business_street = "305 RICHARDSON RD", 
        business_street2 = NA_character_, business_phone = "2158222929"), row.names = c(NA, 
    -1L), class = "data.frame"), filings = structure(list(accession_number = c("0000004911-95-000015", 
    "0000004911-94-000016"), act = c(NA_character_, NA_character_
    ), file_number = c("000-00230", "000-00230"), filing_date = structure(c(800661600, 
    769212000), class = c("POSIXct", "POSIXt"), tzone = ""), 
        accepted_date = structure(c(800661600, 769212000), class = c("POSIXct", 
        "POSIXt"), tzone = ""), href = c("https://www.sec.gov/Archives/edgar/data/4911/0000004911-95-000015-index.html", 
        "https://www.sec.gov/Archives/edgar/data/4911/0000004911-94-000016-index.html"
        ), type = c("10-K", "10-K"), film_number = c("95540616", 
        "94529234"), form_name = c("Annual report [Section 13 and 15(d), not S-K Item 405]", 
        "Annual report [Section 13 and 15(d), not S-K Item 405]"
        ), description = c(NA_character_, NA_character_), size = c("193 KB", 
        "123 KB")), row.names = c(NA, -2L), class = "data.frame")))

EDIT:

Okay I realise the problem is probably down to both elements in the list having different column names. Running:

x <- data$ 0000001750 $information y <- data$ 0000001750 $filings

gives two different data.frames. So I think it is best to combine all the information data frames and then all the filings data frames, so there will be 2 data frames.

I think this solved my issue now:

info <- do.call("rbind", lapply(data, "[[", 1))
filing <- do.call("rbind", lapply(data, "[[", 2))

However I would like to merge the two datasets together.

I think this works:

x <- filing %>%
  rownames_to_column(., "cik") %>%
  mutate(cik = gsub("\\..*", "", cik)) %>%
  left_join(., info, by = "cik")

Actually, you may want to reverse the order in your dplyr::left_join since info contains more clk than filing . The latter maintains two empty data frames in original listing.

library(dplyr)
library(tibble)

info <- do.call("rbind", lapply(data, "[[", 1))
filing <- do.call("rbind", lapply(data, "[[", 2))

final_df_op <- info %>%
                  left_join(filing %>%
                            tibble::rownames_to_column(., "cik") %>%
                            mutate(cik = gsub("\\..*", "", cik)), by = "cik")

str(final_df_op)
# 'data.frame': 51 obs. of  30 variables:
#  $ name               : chr  "AAR CORP" "AAR CORP" "AAR CORP" "AAR CORP" ...
#  $ cik                : chr  "0000001750" "0000001750" "0000001750" "0000001750" ...
#  $ fiscal_year_end    : chr  "0531" "0531" "0531" "0531" ...
# ...

Should you be interested, consider the base R counterpart with following changes:

  • Instead of lapply , use mapply to iterate elementwise through the data items and their corresponding names;

  • Run transform to add a column for cik by corresponding list name;

  • Merge both objects with all.x=TRUE for left join specification.

Base R

info <- do.call("rbind", mapply(function(d, n) transform(d[[2]], cik=n),
                                data, names(data), SIMPLIFY=FALSE, USE.NAMES=FALSE))

# TRY CATCH TO ACCOUNT FOR ZERO-ROW DF ERRORS    
filing <- do.call("rbind", mapply(function(d, n) 
                                  tryCatch(transform(d[[2]], cik=n),
                                           error = function(e) NA),
                                  data, names(data), SIMPLIFY=FALSE, USE.NAMES=FALSE))

# LEFT JOIN MERGE
final_df <- merge(info, filing, by="cik", all.x=TRUE)

str(final_df)
# 'data.frame': 51 obs. of  30 variables:
#  $ cik                : chr  "0000001750" "0000001750" "0000001750" "0000001750" ...
#  $ name               : chr  "AAR CORP" "AAR CORP" "AAR CORP" "AAR CORP" ...
#  $ fiscal_year_end    : chr  "0531" "0531" "0531" "0531" ...
#  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM