Home > Net >  How to remove name with NA in a named list
How to remove name with NA in a named list

Time:12-20

I have the following named list:

my_nl <- c(R = 0.0454545454545455, K = 0.204545454545455, `NA's` = 0.75)

That looks like this:

> my_nl
         R          K       NA's 
0.04545455 0.20454545 0.75000000 

I want to remove a member of that list with NA's as members. Yielding:

         R          K     
0.04545455 0.20454545 

How can I achieve that?

CodePudding user response:

If you want to do it 'by name', one option is:

my_nl <- c(R = 0.0454545454545455, K = 0.204545454545455, `NA's` = 0.75)
my_nl[names(my_nl) != "NA's"]
#>          R          K 
#> 0.04545455 0.20454545

Created on 2022-12-20 with reprex v2.0.2

CodePudding user response:

You can remove using the indexes from the vector, since it is the third element you can either use - to remove it or selecting the other values.

my_nl <- c(R = 0.0454545454545455, K = 0.204545454545455, `NA's` = 0.75)

#Option 1
my_nl[-3]

#Option 2
my_nl[1:2]
  • Related