Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In R, is there a way to handle NA in an integer column of a data.frame so that NA values are not included when subseting?
- (t=structure(list(count = c(NA, 2, NA, NA, NA, 8, NA, NA, NA)), .Names = "count", row.names = c(NA,-9L), class = "data.frame"))
- count
- 1 NA
- 2 2
- 3 NA
- 4 NA
- 5 NA
- 6 8
- 7 NA
- 8 NA
- 9 NA
- > t[t$count>=1,]
- [1] NA 2 NA NA NA 8 NA NA NA
- > t[t$count>=1&!is.na(t$count),]
- [1] 2 8
- transform(t, replace all NAs from count columns with 0)
- transform(t, replace all NA from all numeric columns with 0 in t)
- > t[ which(t$count >= 1), ]
- [1] 2 8
- # Or if you still want a dataframe result
- > t[ which(t$count >= 1), , drop=FALSE]
- count
- 2 2
- 6 8
- > t[ -which(t$count < 1), , drop=FALSE]
- [1] count
- <0 rows> (or 0-length row.names)
- > t[ which(t$count < 1), , drop=FALSE]
- [1] count
- <0 rows> (or 0-length row.names)
- t$count[is.na(t$count)] <- something.else
- dt = as.data.table(t)
- t[count>=1] # NA's are treated as FALSE
Advertisement
Add Comment
Please, Sign In to add comment