blast00
blast00

Reputation: 559

listwise deletion of rows with NA in a given column

I am trying to delete rows in a dataframe where a given column has an NA for that row. For example, take the dataframe below

ID# Name Sales Price 
1    NA    15   34
2    Jay   NA   52
3    Roy   NA   21
4    NA    56   NA
5    Jake  60   20

I want to remove all rows, where the name column has an NA. So I would be left with a dataframe that looks like this:

ID# Name Sales Price 
2    Jay   NA   52
3    Roy   NA   21
5    Jake  60   20

I was trying new_df <- na.omit(df[,"Name"]) but that does not work (not sure why)

Upvotes: 2

Views: 5003

Answers (1)

Robert Krzyzanowski
Robert Krzyzanowski

Reputation: 9344

df <- df[!is.na(df$Name), ]

Example

df <- data.frame(Name = c(NA, "Jay", "Roy", NA, "Jake"), Sales = c(15, NA, NA, 56, 60), Price = c(34, 52, 21, NA, 20), stringsAsFactors = FALSE)
df <- df[!is.na(df$Name), ]
dim(df) # 3 3 

Upvotes: 5

Related Questions