Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all the NA from a Vector? [duplicate]

Tags:

r

Possible Duplicate:
R script - removing NA values from a vector

I could I remove all the NAs from a Vector using R?

[1]  1 NA  3 NA  5 

Thank you

like image 649
Dail Avatar asked Nov 18 '11 15:11

Dail


People also ask

How do I omit Na from a vector in R?

You can try na. omit() or na. exclude() too. It might help you.

How do I omit all NA in R?

To remove all rows having NA, we can use na. omit function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na. omit(df).

How do I remove a value from a vector in R?

Declare a boolean vector that has TRUE at all the positions you want to retain and FALSE at those you want to delete. Suppose that vector is y. Then, x[y] will give you the requires output.

How do I remove Na from a column in R?

To remove observations with missing values in at least one column, you can use the na. omit() function. The na. omit() function in the R language inspects all columns from a data frame and drops rows that have NA's in one or more columns.


1 Answers

Use is.na with vector indexing

x <- c(NA, 3, NA, 5) x[!is.na(x)] [1] 3 5 

I also refer the honourable gentleman / lady to the excellent R introductory manuals, in particular Section 2.7 Index vectors; selecting and modifying subsets of a data set

like image 146
Andrie Avatar answered Sep 23 '22 00:09

Andrie