Reputation: 51
How can I create a new data set based on NA value.
Date Symbol Close
1 2005-01-03 01:20:00 A 115.87094
2 2005-01-03 01:25:00 A 115.88168
3 2005-01-03 01:30:00 A 115.88168
4 2005-01-03 01:35:00 A NA
5 2005-01-03 01:40:00 NA 115.87094
6 2005-01-03 01:45:00 NA 115.87094
7 2005-01-03 01:50:00 A 115.86020
i just want to keep the rows with NA in the row
Date Symbol Close
4 2005-01-03 01:35:00 A NA
5 2005-01-03 01:40:00 NA 115.87094
6 2005-01-03 01:45:00 NA 115.87094
Upvotes: 1
Views: 601
Reputation: 177
I'm relatively a beginner but you could iterate through the rows (not sure about how you might do it with apply or other vectorized functions) using anyNA() on the row and then if it is true keep the row.
EDIT: e.g.
a <- rep(NA, nrow(df))
for (i in 1:nrow(df)) {
a[i] <- anyNA(df[i, ])
}
newdf <- df[a, ]
Generally style guides recommend against over using loops, but the anyNA() and is.na() functions are super useful so worth knowing anyways.
Upvotes: -1
Reputation: 887048
We can use complete.cases
df1[!complete.cases(df1),]
# Date Symbol Close
#4 2005-01-03 01:35:00 A NA
#5 2005-01-03 01:40:00 <NA> 115.8709
#6 2005-01-03 01:45:00 <NA> 115.8709
Upvotes: 3