Pergunta

Suppose that we have the following data frame:

>  dataset1
      x
  1   1
  2   2
  3   3
  4   NA
  5   5

I want to come up with a R command that computes the row index of the 1-column data frame that contains the value of 'NA'. More specifically, in above dataset1 example, such command would return 4 - because the 'NA' appears in the 4th row of the data frame. How can I make this happen? thank you!

Foi útil?

Solução

As suggested by Ben Bolker, you can use both which and is.na as in:

> which(is.na(dataset1), arr.ind=TRUE)
  row col
4   4   1  # NA is in row 4 and column 1

Outras dicas

An alternative approach using functions from the tidyverse ecosystem:

> dataset1 %>%
     rowid_to_column() %>%
     filter(is.na(x))
  rowid  x
1     4 NA

create newdataset1 which is a table formed after deleting rows with missing column values from dataset1 use -which(is.na)

   newdataset1<-dataset1[-which(is.na(dataset1$x)),]

This code returns dataframe which contains only rows with an empty values in your_dataframe your_dataframe[unique(which(is.na(your_dataframe), arr.ind=TRUE)[,1]),]

or using dplyr

your_dataframe %>% dplyr::setdiff(., na.omit(.))

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top