How to remove NA values in vector in R [duplicate]

给你一囗甜甜゛ 提交于 2019-11-30 17:58:41

And even shorter:

v <- na.omit(v)
January

Try this:

new.v <- v[ !is.na( v ) ]

Either is.na or na.omit are good enough for this situation

x <- c(1,NA,2,NA, 3) # a vector with NA
x[!is.na(x)]  # a vector without NA
[1] 1 2 3

as.numeric(na.omit(x))
[1] 1 2 3

Actually as.numeric applied to na.omit is not necessary as you can tell from Dirk's answer :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!