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!
To find the indexes of the specific value that match the given condition in Pandas dataframe we will use df['Subject'] to match the given values and index. values to find an index of matched value. The result shows us that rows 0,1,2 have the value 'Math' in the Subject column.
Extract rows/columns with missing values in specific columns/rows. You can use the isnull() or isna() method of pandas. DataFrame and Series to check if each element is a missing value or not. isnull() is an alias for isna() , whose usage is the same.
You can try as. numeric(rownames(df)) if you haven't set the rownames. Otherwise use a sequence of 1:nrow(df) . The which() function converts a TRUE/FALSE row index into row numbers.
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
An alternative approach using functions from the tidyverse
ecosystem:
> dataset1 %>% rowid_to_column() %>% filter(is.na(x)) rowid x 1 4 NA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With