littleworth
littleworth

Reputation: 5169

How to remove name with NA in a named list

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?

Upvotes: 1

Views: 123

Answers (2)

Vin&#237;cius F&#233;lix
Vin&#237;cius F&#233;lix

Reputation: 8811

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]

Upvotes: 0

jared_mamrot
jared_mamrot

Reputation: 26495

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

Upvotes: 2

Related Questions