从列表列表和空数据帧中提取元素

时间:2017-07-12 14:31:17

标签: r

我有一个丑陋的列表和空数据框列表(api有趣!)。我想要的是name元素的向量。在这种情况下,向量的长度应为13,其中NA为data frame with 0 columns and 0 rows

myList <- list(structure(list(uuid = "x1", 
    name = "thanks"), .Names = c("uuid", "name"), class = "data.frame", row.names = 1L), 
    structure(list(uuid = "x2", 
        name = "team"), .Names = c("uuid", "name"), class = "data.frame", row.names = 1L), 
    structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"), 
    structure(list(uuid = "x3", 
        name = "team"), .Names = c("uuid", "name"), class = "data.frame", row.names = 1L), 
    structure(list(uuid = "x4", 
        name = "team"), .Names = c("uuid", "name"), class = "data.frame", row.names = 1L), 
    structure(list(uuid = "x5", 
        name = "enrolled"), .Names = c("uuid", "name"), class = "data.frame", row.names = 1L), 
    structure(list(uuid = "x6", 
        name = "team"), .Names = c("uuid", "name"), class = "data.frame", row.names = 1L), 
    structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"), 
    structure(list(uuid = "x7", 
        name = "team"), .Names = c("uuid", "name"), class = "data.frame", row.names = 1L), 
    structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"), 
    structure(list(uuid = "x8", 
        name = "team"), .Names = c("uuid", "name"), class = "data.frame", row.names = 1L), 
    structure(list(uuid = "x9", 
        name = "team"), .Names = c("uuid", "name"), class = "data.frame", row.names = 1L), 
    structure(list(uuid = "x10", 
        name = "team"), .Names = c("uuid", "name"), class = "data.frame", row.names = 1L))

期望的输出:

"thanks" "team" NA "team" "team" "enrolled" "team" NA "team" NA "team" "team" "team"

1 个答案:

答案 0 :(得分:1)

也许它适合你:

sapply(myList, function(x){
    if(all(dim(x) == c(0,0))){
        x <- NA
    } else x <- x$name
})
相关问题