anrpet
anrpet

Reputation: 139

How to remove elements with NA

I am using this function to return a list of pairs from a data frame:

f <- function(x, n) sort(table(x), decreasing = TRUE)[1:n]


lapply(mypairlist, f, n = 3)

An example output:

V18, NA     A12, NA     S21, NA 
290         155         45 

I do not want the function to return pairs that have NA in them. I'm sure it's really simple but I can't figure out how to do this. Any help would be greatly appreciated.

My input data looks like this:

ID       TopPair
123      V18, NA
124      V18, NA
125      V18, V21
126      A12, NA
127      A12, NA
128      V18, NA
129      A57, B43

And I want V18, V21 and A57, B43 returned rather than pairs with NAs.

Upvotes: 2

Views: 43

Answers (1)

akrun
akrun

Reputation: 887128

Assuming that the 'TopPair' is a string column, we can use grep to get the index of NA elements

df1[!grepl("NA", df1$TopPair),]
#   ID  TopPair
#3 125 V18, V21
#7 129 A57, B43

Upvotes: 4

Related Questions