3

So I have a block of text that I've seperated into a vector, and from each line of vector I've further seperated it into a data frame. In a perfect world, every row of the DF would be exactly the same, but it's not and there a number of rows with NA values in them. What I need to do is select the row from the data frame with the least number of NA values.

So say the DF looked like this:

Name Year NA Address NA State NA
Name Year ID Address City State Rank
Name Year NA NA City State NA
Name NA NA NA NA NA Rank
Name Year NA NA NA NA NA

Where they each belong to column. So I need a way to identify which row has the least number of NA's, and then select that row's elements. So ultimately I want the return to just be single row DF (or a vector preferably) that reads

Name Year ID Address City State Rank

In this case, row 2.

I know that:

max( rowSums(!is.na(x)) )

Will return me the row# with the most number of not-na values, but I can't seem to figure out how to grab the elements of that row. I was thinking using which() would work, but I can't seem to figure it out.

Thanks for your help!

David

4

1 回答 1

7

如果您的数据框是df,则:

df[which.max(rowSums(!is.na(df))),]

应该返回具有最少 NA 的单行数据帧。

于 2013-06-03T20:09:49.120 回答