littleworth
littleworth

Reputation: 5169

How can I remove member of list of list that contains NA as its name

I have the following list of list:

x <- structure(list(NULL, NULL, list(aucThr = list(selected = c(L_k2 = 0.174126420682375), 
    thresholds = structure(c(0.0487179487179487, 0.131412403874156, 
    0.174126420682375, 6229, 2398, 1408), .Dim = 3:2, .Dimnames = list(
        c("tenPercentOfMax", "Global_k1", "L_k2"), c("threshold", 
        "nCells"))), comment = ""), assignment = c("R200312_NB501621_0595_AHYFKWBGXC_SCI193_01_GCGGTTGCATCC", 
"R200312_NB501621_0595_AHYFKWBGXC_SCI193_01_CCGTGGTCATCC", "R200312_NB501621_0595_AHYFKWBGXC_SCI193_01_ACTAGAGGATCC"
))), .Names = c(NA, NA, "Arntl (13g)"))

That looks like this:

> x
$<NA>
NULL

$<NA>
NULL

$`Arntl (13g)`
$`Arntl (13g)`$aucThr
$`Arntl (13g)`$aucThr$selected
     L_k2 
0.1741264 

$`Arntl (13g)`$aucThr$thresholds
                 threshold nCells
tenPercentOfMax 0.04871795   6229
Global_k1       0.13141240   2398
L_k2            0.17412642   1408

$`Arntl (13g)`$aucThr$comment
[1] ""


$`Arntl (13g)`$assignment
[1] "R200312_NB501621_0595_AHYFKWBGXC_SCI193_01_GCGGTTGCATCC"
[2] "R200312_NB501621_0595_AHYFKWBGXC_SCI193_01_CCGTGGTCATCC"
[3] "R200312_NB501621_0595_AHYFKWBGXC_SCI193_01_ACTAGAGGATCC"

What I want to do is to remove the member that looks like this:

$<NA>
NULL

How can I achieve that? The desired output is same structure with only 1 content. Arntl (13g)

Upvotes: 0

Views: 36

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388862

You can use lengths :

x[lengths(x) > 1]

#$`Arntl (13g)`
#$`Arntl (13g)`$aucThr
#$`Arntl (13g)`$aucThr$selected
#     L_k2 
#0.1741264 

#$`Arntl (13g)`$aucThr$thresholds
#                 threshold nCells
#tenPercentOfMax 0.04871795   6229
#Global_k1       0.13141240   2398
#L_k2            0.17412642   1408

#$`Arntl (13g)`$aucThr$comment
#[1] ""


#$`Arntl (13g)`$assignment
#[1] "R200312_NB501621_0595_AHYFKWBGXC_SCI193_01_GCGGTTGCATCC"
#[2] "R200312_NB501621_0595_AHYFKWBGXC_SCI193_01_CCGTGGTCATCC"
#[3] "R200312_NB501621_0595_AHYFKWBGXC_SCI193_01_ACTAGAGGATCC"

Or subset based on names :

x[!is.na(names(x))]

Upvotes: 1

Related Questions