【问题标题】:Print out items from a list in R with different nested levels从 R 中的列表中打印出具有不同嵌套级别的项目
【发布时间】:2019-10-20 15:16:36
【问题描述】:

我在 R 中有一个列表,其结构与 dput 输出中的结构相同。

更新:

structure(list(name = c("\\text{Students are able to identify relationship between l (or ml) and cm³\n}", 
"\\text{Students are able to write down the notation and representations of fractions \n}", 
"\\text{Scientific enquiry}", "\\text{Polska w okresie rozbicia dzielnicowego.}"
), index = c(26L, 39L, 0L, 5L), learningFactorType = c("LEARNING_OBJECTIVE", 
"LEARNING_OBJECTIVE", "LEARNING_OBJECTIVE", "LEARNING_OBJECTIVE"
), childFactors = list(structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"), 
    structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"), 
    structure(list(`_id` = list(structure(as.raw(c(0xee, 0x41, 
    0x32, 0x09, 0x81, 0x82, 0xf4, 0xac, 0x27, 0x49, 0xc2, 0x0b, 
    0x70, 0x65, 0x05, 0x89)), type = as.raw(0x03)), structure(as.raw(c(0xdc, 
    0x46, 0xaf, 0x85, 0x60, 0x2b, 0x6a, 0x04, 0x15, 0x48, 0x7c, 
    0xa5, 0xd0, 0xbc, 0x27, 0x8b)), type = as.raw(0x03)), structure(as.raw(c(0x76, 
    0x4a, 0x90, 0xc0, 0x90, 0xc6, 0xf1, 0x76, 0x18, 0x51, 0x13, 
    0xc1, 0xae, 0xa1, 0x03, 0x84)), type = as.raw(0x03)), structure(as.raw(c(0xec, 
    0x42, 0x98, 0x7a, 0xd4, 0x16, 0x23, 0x3f, 0x43, 0xfd, 0x36, 
    0x5d, 0xa1, 0x4a, 0xcd, 0xa8)), type = as.raw(0x03))), name = c("\\text{ Ideas and evidence}", 
    "\\text{Plan investigative work}", "\\text{Obtain and present evidence}", 
    "\\text{ Consider evidence and approach}"), index = 0:3, 
        childFactors = list(structure(list(`_id` = list(structure(as.raw(c(0xee, 
        0x49, 0x92, 0x4f, 0xe2, 0x10, 0xb8, 0x33, 0x6f, 0xbe, 
        0xd2, 0x9f, 0x46, 0x82, 0x36, 0x9d)), type = as.raw(0x03)), 
            structure(as.raw(c(0xa2, 0x4e, 0x9c, 0x07, 0x44, 
            0x1b, 0x7b, 0x44, 0xa3, 0x03, 0xe9, 0x22, 0x69, 0xaf, 
            0x5b, 0x85)), type = as.raw(0x03))), name = c("\\text{Collect evidence in a variety of contexts (4Ep1)}", 
        "\\text{Test an idea or prediction based on scientific knowledge and understanding (4Ep2)}"
        ), index = 0:1, childFactors = list(list(), list()), 
            exerciseIdList = list(list(), list()), knowledgeBaseEntryIdList = list(
                list(), list()), linkedSourceList = list(structure(list(
                publisherId = c(193, 193, 193, 193, 193, 193), 
                categoryId = c(22610, 22610, 22610, 22612, 22614, 
                22614), levelId = c(1001, 1001, 1001, 1001, 1001, 
                1001), subcategoryIdList = list(22618, 22619, 
                  22620, 22628, 22631, 22634), exerciseIdList = list(
                  c(281313, 281322, 281439, 281442), c(281310, 
                  281312, 281437), c(281307, 281324), c(325294, 
                  325296, 325380, 325394, 325448, 325473, 325516
                  ), c(328850, 328851, 328853, 328856, 328861, 
                  328869, 328870, 328871), c(328854, 335329)), 
                knowledgeBaseEntryIdList = list(list(), list(), 
                  list(), list(), list(), list())), class = "data.frame", row.names = c(NA, 
            6L)), structure(list(publisherId = c(193, 193), categoryId = c(22614, 
            22614), levelId = c(1001, 1001), subcategoryIdList = list(
                22632, 22633), exerciseIdList = list(c(328852, 
            335327), c(328865, 335324)), knowledgeBaseEntryIdList = list(
                list(), list())), class = "data.frame", row.names = 1:2))), class = "data.frame", row.names = 1:2), 
            structure(list(`_id` = list(structure(as.raw(c(0x43, 
            0x46, 0xbd, 0xa0, 0x8f, 0xf6, 0x37, 0xad, 0x48, 0x37, 
            0xeb, 0x2f, 0x6f, 0xfe, 0x34, 0xb4)), type = as.raw(0x03)), 
                structure(as.raw(c(0xca, 0x4b, 0x33, 0x04, 0x0b, 
                0x2f, 0x1d, 0x75, 0xc6, 0x84, 0xaa, 0xff, 0xfb, 
                0x4c, 0x95, 0xa8)), type = as.raw(0x03)), structure(as.raw(c(0xcc, 
                0x45, 0xa4, 0xec, 0x0a, 0xb1, 0x5a, 0x02, 0x29, 
                0xf2, 0x58, 0x21, 0x58, 0x29, 0xab, 0x8a)), type = as.raw(0x03))), 
                name = c("\\text{Suggest questions that can be tested and make predictions; \ncommunicate these (4Ep3)}", 
                "\\text{Design a fair test and plan how to collect sufficient evidence (4Ep4)}", 
                "\\text{Choose apparatus and decide what to measure (4Ep5)}"
                ), index = 0:2, childFactors = list(list(), list(), 
                  list()), exerciseIdList = list(list(), list(), 
                  list()), knowledgeBaseEntryIdList = list(list(), 
                  list(), list()), linkedSourceList = list(structure(list(
                  publisherId = c(193, 193, 193, 193), categoryId = c(22611, 
                  22611, 22615, 22615), levelId = c(1001, 1001, 
                  1001, 1001), subcategoryIdList = list(22624, 
                    22625, 22635, 22637), exerciseIdList = list(
                    c(325414, 325415, 325420, 325433), c(325416, 
                    325417, 325418, 325419, 325423, 325427, 325430, 
                    325434, 334617, 334647, 334658), c(328887, 
                    328891), c(328895, 328898, 328904)), knowledgeBaseEntryIdList = list(
                    list(), list(), list(), list())), class = "data.frame", row.names = c(NA, 
                4L)), structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"), 
                  structure(list(publisherId = 193, categoryId = 22611, 
                    levelId = 1001, subcategoryIdList = list(
                      22625), exerciseIdList = list(c(325416, 
                    325417, 325418, 325419, 325423, 325427, 325430, 
                    325434, 334617, 334647, 334658)), knowledgeBaseEntryIdList = list(
                      list())), class = "data.frame", row.names = 1L))), class = "data.frame", row.names = c(NA, 
            3L)), structure(list(`_id` = list(structure(as.raw(c(0xf1, 
            0x41, 0xd0, 0xff, 0x4c, 0xaf, 0x1a, 0x7c, 0x8f, 0x4e, 
            0x8a, 0xdb, 0xdb, 0xd2, 0x87, 0xa7)), type = as.raw(0x03)), 
                structure(as.raw(c(0x7f, 0x42, 0xce, 0xf2, 0x3f, 
                0xe4, 0xc3, 0x9f, 0xb7, 0x17, 0x52, 0xfe, 0xb3, 
                0x8a, 0xc9, 0x89)), type = as.raw(0x03)), structure(as.raw(c(0x77, 
                0x49, 0xcc, 0x51, 0xb0, 0x85, 0x03, 0xf5, 0x72, 
                0xeb, 0x2f, 0xe5, 0x6e, 0x32, 0x53, 0x8b)), type = as.raw(0x03)), 
                structure(as.raw(c(0xe1, 0x40, 0x24, 0xb9, 0x1e, 
                0x32, 0xbe, 0xca, 0x33, 0x5e, 0x9e, 0x3d, 0xd0, 
                0x2f, 0x1a, 0x87)), type = as.raw(0x03))), name = c("\\text{Make relevant observations and comparisons in a variety of \ncontexts (4Eo1)}", 
            "\\text{Measure temperature, time, force and length (4Eo2)}", 
            "\\text{Begin to think about the need for repeated measurements of, \nfor example, length (4Eo3)}", 
            "\\text{Present results in drawings, bar charts and tables (4Eo4)}"
            ), index = 0:3, childFactors = list(list(), list(), 
                list(), list()), exerciseIdList = list(list(), 
                list(), list(), list()), knowledgeBaseEntryIdList = list(
                list(), list(), list(), list()), linkedSourceList = list(
                structure(list(publisherId = c(193, 193, 193, 
                193, 193), categoryId = c(22610, 22610, 22610, 
                22612, 22614), levelId = c(1001, 1001, 1001, 
                1001, 1001), subcategoryIdList = list(22619, 
                  22620, 22621, 22628, 22631), exerciseIdList = list(
                  c(281310, 281312, 281437), c(281307, 281324
                  ), c(281308, 281326), c(325294, 325296, 325380, 
                  325394, 325448, 325473, 325516), c(328850, 
                  328851, 328853, 328856, 328861, 328869, 328870, 
                  328871)), knowledgeBaseEntryIdList = list(list(), 
                  list(), list(), list(), list())), class = "data.frame", row.names = c(NA, 
                5L)), structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"), 
                structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"), 
                structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"))), class = "data.frame", row.names = c(NA, 
            4L)), structure(list(`_id` = list(structure(as.raw(c(0xc1, 
            0x41, 0x64, 0x24, 0x4d, 0x96, 0x1d, 0xc5, 0x4b, 0x18, 
            0x9f, 0xc3, 0xe2, 0xfe, 0x82, 0xb9)), type = as.raw(0x03)), 
                structure(as.raw(c(0x8a, 0x44, 0x8a, 0x41, 0xed, 
                0x1d, 0x70, 0xdc, 0x81, 0x96, 0x29, 0x0a, 0x49, 
                0xe8, 0x73, 0xa3)), type = as.raw(0x03)), structure(as.raw(c(0x9d, 
                0x4a, 0x23, 0x7c, 0x2a, 0x57, 0x55, 0x31, 0xca, 
                0xd4, 0x17, 0xff, 0x32, 0x9b, 0xfa, 0x9c)), type = as.raw(0x03))), 
                name = c("\\text{Identify simple trends and patterns in results and suggest \nexplanations for some of these (4Eo5)}", 
                "\\text{Explain what the evidence shows and whether it supports \npredictions.  Communicate this clearly to others (4Eo6)}", 
                "\\text{Link evidence to scientific knowledge and understanding in \nsome contexts (4Eo7)}"
                ), index = 0:2, childFactors = list(list(), list(), 
                  list()), exerciseIdList = list(list(), list(), 
                  list()), knowledgeBaseEntryIdList = list(list(), 
                  list(), list()), linkedSourceList = list(structure(list(
                  publisherId = c(193, 193, 193, 193), categoryId = c(22611, 
                  22614, 22614, 22615), levelId = c(1001, 1001, 
                  1001, 1001), subcategoryIdList = list(22625, 
                    22632, 22633, 22636), exerciseIdList = list(
                    c(325416, 325417, 325418, 325419, 325423, 
                    325427, 325430, 325434, 334617, 334647, 334658
                    ), c(328852, 335327), c(328865, 335324), 
                    c(328886, 328888, 328903)), knowledgeBaseEntryIdList = list(
                    list(), list(), list(), list())), class = "data.frame", row.names = c(NA, 
                4L)), structure(list(publisherId = 193, categoryId = 22615, 
                  levelId = 1001, subcategoryIdList = list(22637), 
                  exerciseIdList = list(c(328895, 328898, 328904
                  )), knowledgeBaseEntryIdList = list(list())), class = "data.frame", row.names = 1L), 
                  structure(list(publisherId = c(193, 193, 193
                  ), categoryId = c(22612, 22614, 22614), levelId = c(1001, 
                  1001, 1001), subcategoryIdList = list(22628, 
                    22633, 22634), exerciseIdList = list(c(325294, 
                  325296, 325380, 325394, 325448, 325473, 325516
                  ), c(328865, 335324), c(328854, 335329)), knowledgeBaseEntryIdList = list(
                    list(), list(), list())), class = "data.frame", row.names = c(NA, 
                  3L)))), class = "data.frame", row.names = c(NA, 
            3L))), exerciseIdList = list(list(), list(), list(), 
            list()), knowledgeBaseEntryIdList = list(list(), 
            list(), list(), list()), linkedSourceList = list(
            structure(list(publisherId = c(193, 193, 193, 193, 
            193, 193, 193, 193), categoryId = c(22610, 22610, 
            22610, 22612, 22614, 22614, 22614, 22614), levelId = c(1001, 
            1001, 1001, 1001, 1001, 1001, 1001, 1001), subcategoryIdList = list(
                22618, 22619, 22620, 22628, 22631, 22634, 22632, 
                22633), exerciseIdList = list(c(281313, 281322, 
            281439, 281442), c(281310, 281312, 281437), c(281307, 
            281324), c(325294, 325296, 325380, 325394, 325448, 
            325473, 325516), c(328850, 328851, 328853, 328856, 
            328861, 328869, 328870, 328871), c(328854, 335329
            ), c(328852, 335327), c(328865, 335324)), knowledgeBaseEntryIdList = list(
                list(), list(), list(), list(), list(), list(), 
                list(), list())), class = "data.frame", row.names = c(NA, 
            8L)), structure(list(publisherId = c(193, 193, 193, 
            193, 193), categoryId = c(22611, 22611, 22615, 22615, 
            22611), levelId = c(1001, 1001, 1001, 1001, 1001), 
                subcategoryIdList = list(22624, 22625, 22635, 
                  22637, 22625), exerciseIdList = list(c(325414, 
                325415, 325420, 325433), c(325416, 325417, 325418, 
                325419, 325423, 325427, 325430, 325434, 334617, 
                334647, 334658), c(328887, 328891), c(328895, 
                328898, 328904), c(325416, 325417, 325418, 325419, 
                325423, 325427, 325430, 325434, 334617, 334647, 
                334658)), knowledgeBaseEntryIdList = list(list(), 
                  list(), list(), list(), list())), class = "data.frame", row.names = c(NA, 
            5L)), structure(list(publisherId = c(193, 193, 193, 
            193, 193), categoryId = c(22610, 22610, 22610, 22612, 
            22614), levelId = c(1001, 1001, 1001, 1001, 1001), 
                subcategoryIdList = list(22619, 22620, 22621, 
                  22628, 22631), exerciseIdList = list(c(281310, 
                281312, 281437), c(281307, 281324), c(281308, 
                281326), c(325294, 325296, 325380, 325394, 325448, 
                325473, 325516), c(328850, 328851, 328853, 328856, 
                328861, 328869, 328870, 328871)), knowledgeBaseEntryIdList = list(
                  list(), list(), list(), list(), list())), class = "data.frame", row.names = c(NA, 
            5L)), structure(list(publisherId = c(193, 193, 193, 
            193, 193, 193, 193, 193), categoryId = c(22611, 22614, 
            22614, 22615, 22615, 22612, 22614, 22614), levelId = c(1001, 
            1001, 1001, 1001, 1001, 1001, 1001, 1001), subcategoryIdList = list(
                22625, 22632, 22633, 22636, 22637, 22628, 22633, 
                22634), exerciseIdList = list(c(325416, 325417, 
            325418, 325419, 325423, 325427, 325430, 325434, 334617, 
            334647, 334658), c(328852, 335327), c(328865, 335324
            ), c(328886, 328888, 328903), c(328895, 328898, 328904
            ), c(325294, 325296, 325380, 325394, 325448, 325473, 
            325516), c(328865, 335324), c(328854, 335329)), knowledgeBaseEntryIdList = list(
                list(), list(), list(), list(), list(), list(), 
                list(), list())), class = "data.frame", row.names = c(NA, 
            8L)))), class = "data.frame", row.names = c(NA, 4L
    )), structure(list(`_id` = list(structure(as.raw(c(0x76, 
    0x4f, 0x14, 0xee, 0xb1, 0x19, 0x89, 0xcd, 0x04, 0xd3, 0x44, 
    0x33, 0xa9, 0x1a, 0x6a, 0xbc)), type = as.raw(0x03)), structure(as.raw(c(0x3d, 
    0x4d, 0x57, 0x27, 0x96, 0x1a, 0x86, 0xc0, 0xf4, 0x27, 0x5b, 
    0x2d, 0x19, 0x32, 0xd2, 0x86)), type = as.raw(0x03)), structure(as.raw(c(0x44, 
    0x47, 0xac, 0x85, 0x90, 0x69, 0x04, 0x27, 0x68, 0x4d, 0x12, 
    0x1a, 0x79, 0xfd, 0x76, 0xa6)), type = as.raw(0x03)), structure(as.raw(c(0x4f, 
    0x49, 0x90, 0xd5, 0x40, 0x9c, 0x66, 0xe5, 0x76, 0x22, 0x05, 
    0x7a, 0xaf, 0xd2, 0x95, 0xac)), type = as.raw(0x03)), structure(as.raw(c(0x8c, 
    0x45, 0xfa, 0x87, 0xc8, 0x74, 0x08, 0xaf, 0xba, 0x3e, 0xb1, 
    0x22, 0xb4, 0x91, 0xa0, 0xa1)), type = as.raw(0x03))), name = c("\\text{Umieszczanie w czasie i przestrzeni Polski okresu rozbicia dzielnicowego.}", 
    "\\text{Opisywanie przyczyn oraz wskazywanie skutków rozbicia dzielnicowego.}", 
    "\\text{Umieszczanie w czasie najważniejszych wydarzeń związanych z relacjami polsko-krzyżackimi oraz zagrożeniem najazdami tatarskimi w okresie rozbicia dzielnicowego.}", 
    "\\text{Opisywanie przemiany społecznej i gospodarczej, z uwzględnieniem ruchu osadniczego.}", 
    "\\text{Charakteryzowanie procesu zjednoczenia państwa polskiego na przełomie XIII i XIV wieku, wskazując na rolę władców piastowskich (ze szczególnym uwzględnieniem roli Władysława Łokietka) oraz Kościoła.}"
    ), index = 0:4, childFactors = list(list(), list(), list(), 
        list(), list()), exerciseIdList = list(list(), list(), 
        list(), list(), list()), knowledgeBaseEntryIdList = list(
        list(), list(), list(), list(), list()), linkedSourceList = list(
        list(), list(), list(), list(), list())), class = "data.frame", row.names = c(NA, 
    5L))), exerciseIdList = list(list(), list(), list(), list()), 
    levelIdSet = list(462, 459, 1001, c(34, 857, 877)), knowledgeBaseEntryIdList = list(
        list(), list(), list(), list()), subjectId = c(49, 49, 
    357, 9), linkedSourceList = list(structure(list(publisherId = 111, 
        categoryId = 42281, levelId = 462, subcategoryIdList = list(
            42284), exerciseIdList = list(c(495048, 495047, 495043, 
        495044, 495045, 495046, 495049, 495050)), knowledgeBaseEntryIdList = list(
            list())), class = "data.frame", row.names = 1L), 
        structure(list(publisherId = 111, categoryId = 41394, 
            levelId = 459, subcategoryIdList = list(41395), exerciseIdList = list(
                c(473272, 473270, 473269, 473262, 473261, 465453, 
                465452)), knowledgeBaseEntryIdList = list(list())), class = "data.frame", row.names = 1L), 
        structure(list(publisherId = c(193, 193, 193, 193, 193, 
        193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 
        193, 193, 193, 193, 193, 193, 193, 193, 193, 193), categoryId = c(22610, 
        22610, 22610, 22612, 22614, 22614, 22614, 22614, 22611, 
        22611, 22615, 22615, 22611, 22610, 22610, 22610, 22612, 
        22614, 22611, 22614, 22614, 22615, 22615, 22612, 22614, 
        22614), levelId = c(1001, 1001, 1001, 1001, 1001, 1001, 
        1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 
        1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 1001, 
        1001, 1001), subcategoryIdList = list(22618, 22619, 22620, 
            22628, 22631, 22634, 22632, 22633, 22624, 22625, 
            22635, 22637, 22625, 22619, 22620, 22621, 22628, 
            22631, 22625, 22632, 22633, 22636, 22637, 22628, 
            22633, 22634), exerciseIdList = list(c(281313, 281322, 
        281439, 281442), c(281310, 281312, 281437), c(281307, 
        281324), c(325294, 325296, 325380, 325394, 325448, 325473, 
        325516), c(328850, 328851, 328853, 328856, 328861, 328869, 
        328870, 328871), c(328854, 335329), c(328852, 335327), 
            c(328865, 335324), c(325414, 325415, 325420, 325433
            ), c(325416, 325417, 325418, 325419, 325423, 325427, 
            325430, 325434, 334617, 334647, 334658), c(328887, 
            328891), c(328895, 328898, 328904), c(325416, 325417, 
            325418, 325419, 325423, 325427, 325430, 325434, 334617, 
            334647, 334658), c(281310, 281312, 281437), c(281307, 
            281324), c(281308, 281326), c(325294, 325296, 325380, 
            325394, 325448, 325473, 325516), c(328850, 328851, 
            328853, 328856, 328861, 328869, 328870, 328871), 
            c(325416, 325417, 325418, 325419, 325423, 325427, 
            325430, 325434, 334617, 334647, 334658), c(328852, 
            335327), c(328865, 335324), c(328886, 328888, 328903
            ), c(328895, 328898, 328904), c(325294, 325296, 325380, 
            325394, 325448, 325473, 325516), c(328865, 335324
            ), c(328854, 335329)), knowledgeBaseEntryIdList = list(
            list(), list(), list(), list(), list(), list(), list(), 
            list(), list(), list(), list(), list(), list(), list(), 
            list(), list(), list(), list(), list(), list(), list(), 
            list(), list(), list(), list(), list())), class = "data.frame", row.names = c(NA, 
        26L)), structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame")), 
    treeId = list(structure(as.raw(c(0xa6, 0x48, 0x30, 0x23, 
    0xe1, 0xce, 0xc1, 0x0e, 0x4d, 0x56, 0xe0, 0xc6, 0x6d, 0x71, 
    0x51, 0x96)), type = as.raw(0x03)), structure(as.raw(c(0x27, 
    0x4d, 0xc2, 0xf7, 0xcc, 0xcb, 0x8b, 0x82, 0x2f, 0x20, 0x25, 
    0x72, 0x33, 0x14, 0xee, 0xb7)), type = as.raw(0x03)), structure(as.raw(c(0x7d, 
    0x49, 0x2a, 0xf1, 0x7f, 0xde, 0xff, 0x2a, 0x37, 0x11, 0x33, 
    0x09, 0xaa, 0xa4, 0xd9, 0x87)), type = as.raw(0x03)), structure(as.raw(c(0xbb, 
    0x46, 0x98, 0x1b, 0xa8, 0x19, 0xeb, 0x30, 0x1b, 0x35, 0xf6, 
    0x22, 0x0f, 0xb2, 0x4c, 0x99)), type = as.raw(0x03))), version = c(111, 
    123, 104, 224), indexAlias = c(NA_character_, NA_character_, 
    NA_character_, NA_character_)), row.names = c(1L, 2L, 3L, 
8L), class = "data.frame")



我想打印出子列表的每个名称 li[[1]] 以及带有练习 ID 的第五项子子列表 li[[9]][[5]]。

purrr 包让我可以简单地抓取 li[[9]] 的元素,但我不确定如何映射上面相关列表中的名称 li[[1]]。

ex_id<-li[[9]]
df_ex_id <- as.data.frame(unlist(map(ex.id,5)))

我尝试使用 for 函数:

name_exid <- for (i in li$name) {
print(li$name) + print(li$exerciseIdList[[1]])  
}

但是我收到一条错误消息并且失败得很惨:(

我想要实现的结果是我有一个带有名称的列 A 和一个带有 ex.ids 的列 B

【问题讨论】:

  • 我敢打赌这是由 JSON 导入构建的!从技术上讲,li 是一个data.frame(一个特殊的list

标签: r list for-loop purrr


【解决方案1】:

考虑基本 R 的 Map(包装到 mapply)在您需要的项目之间逐元素迭代以绑定到列表或数据框。此外,sapply 可以帮助在li[[9]] 中的所有数据帧中提取所需的嵌套列表。

extract_list <- Map(function(nm, id, ex) list(name=nm, index=id, excerciseIdList=ex),
                    li$name,
                    li$index,
                    sapply(li[[9]], `[[`, "exerciseIdList"), USE.NAMES=FALSE)

extract_list
# [[1]]
# [[1]]$name
# [1] "\\text{Students are able to identify relationship between l (or ml) and cm³\n}"
# 
# [[1]]$index
# [1] 26
# 
# [[1]]$excerciseIdList
# [1] 495048 495047 495043 495044 495045 495046 495049 495050
# 
# 
# [[2]]
# [[2]]$name
# [1] "\\text{Students are able to write down the notation and representations of fractions \n}"
# 
# [[2]]$index
# [1] 39
# 
# [[2]]$excerciseIdList
# [1] 473272 473270 473269 473262 473261 465453 465452

构建数据框:

df_list <- Map(function(nm, id, ex) data.frame(name=nm, index=id, excerciseIdList=ex,
                                               stringsAsFactors = FALSE),
               li$name,
               li$index,
               sapply(li[[9]], `[[`, "exerciseIdList"))

final_df <- do.call(rbind, unname(df_list))

final_df    
#                                                                                        name index excerciseIdList
# 1            \\text{Students are able to identify relationship between l (or ml) and cm³\n}    26          495048
# 2            \\text{Students are able to identify relationship between l (or ml) and cm³\n}    26          495047
# 3            \\text{Students are able to identify relationship between l (or ml) and cm³\n}    26          495043
# 4            \\text{Students are able to identify relationship between l (or ml) and cm³\n}    26          495044
# 5            \\text{Students are able to identify relationship between l (or ml) and cm³\n}    26          495045
# 6            \\text{Students are able to identify relationship between l (or ml) and cm³\n}    26          495046
# 7            \\text{Students are able to identify relationship between l (or ml) and cm³\n}    26          495049
# 8            \\text{Students are able to identify relationship between l (or ml) and cm³\n}    26          495050
# 9  \\text{Students are able to write down the notation and representations of fractions \n}    39          473272
# 10 \\text{Students are able to write down the notation and representations of fractions \n}    39          473270
# 11 \\text{Students are able to write down the notation and representations of fractions \n}    39          473269
# 12 \\text{Students are able to write down the notation and representations of fractions \n}    39          473262
# 13 \\text{Students are able to write down the notation and representations of fractions \n}    39          473261
# 14 \\text{Students are able to write down the notation and representations of fractions \n}    39          465453
# 15 \\text{Students are able to write down the notation and representations of fractions \n}    39          465452

【讨论】:

  • 嗨 Parfait,事实上,这个表是基于 .bson 文件的。您的解决方案工作得非常好,但不幸的是,一旦linkedSourceList 更改其格式(超过一行)或仅获得NULL,我就会收到一条错误消息。我已经更新了 dput 输出。只是出于好奇:final_df 仍然是一个列表,对吧?
【解决方案2】:

更新

这有点挑战性,但它适用于您的新列表。

library(tidyverse)

li %>% 
  map(enframe) %>% 
  `[`(., c("name", "linkedSourceList")) %>% 
  bind_cols() %>% 
  unnest(value1) %>% 
  select(name = "value", exerciseIdList) %>% 
  unnest(exerciseIdList)

旧答案

这是从您的原始列表li 开始的基于管道的方法:

library(tidyverse)

li %>%
  pluck("linkedSourceList") %>%           # get list element called linkedSourceList
  map("exerciseIdList") %>%               # extract elements called "exerciseIdList
  flatten %>%                             # remove hierarchy level
  set_names(li[["name"]]) %>%             # set_names of sub-list
  enframe(value = "excerciseIdList") %>%  # put list in a tibble with a list-column
  unnest(excerciseIdList)                 # unnest list-column

【讨论】:

  • 谢谢蒂姆!这在 dput 中运行良好,但是每当我将其应用于具有 2011 行的基础 df 时,我都会检索到一条错误消息,这可能是由于名为 linkedSourceList 的列表元素有时为 NULL,这意味着不存储任何信息。我已经阅读了 purrr 文档,但我很难理解 set_names 函数。再次感谢您,蒂姆!
  • 嗨蒂姆,我已经更新了我的问题中的 dput 输出。我认为这与linkedSourceList中的空值无关,而与可能存在不止一行的executiveIds有关。
  • 我稍后再看看!
  • 嗨,蒂姆,它运行良好,但将其应用于 2011 年的整个行集,我收到一条错误消息 Error: Argument 1182 can't be converted from list to numeric。我的理解是,这是由各种数据类型引起的。事实上,li$name 看起来像这样structure(list(name = c("\\text{History}", "evaluation")。在大多数情况下,名称也是一个列表。但有时不是。 Row1182 实际上有这种字符串作为名称。但是有些行也有这个字符串并且没有返回错误消息:(无论如何。非常感谢您的帮助!!!
  • 您是否可以在 dput 中包含第 1182 行?
猜你喜欢
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多