Eric Green
Eric Green

Reputation: 7725

extracting element from list of lists and empty dataframes

I have an ugly list of lists and null data frames (api fun!). What I want is a vector of the name element. In this case, the vector should be length 13 with NAs where 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))

Desired output:

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

Upvotes: 0

Views: 62

Answers (1)

Lu&#237;s Telles
Lu&#237;s Telles

Reputation: 694

Maybe it works for you:

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

Upvotes: 1

Related Questions