博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
R: NULL, NA, and NaN
阅读量:7104 次
发布时间:2019-06-28

本文共 1578 字,大约阅读时间需要 5 分钟。

  • NaN (“Not a Number”) means 0/0
  • NA (“Not Available”) is generally interpreted as a missing value and has various forms – NA_integer_, NA_real_, etc. 
  • Therefore, NaN ≠ NA and there is a need for NaN and NA.
  • is.na() returns TRUE for both NA and NaN, however is.nan() return TRUE for NaN (0/0) and FALSE for NA.
  • NULL represents that the value in question simply does not exist, rather than being existent but unknown.

is.na(x) # returns TRUE of x is missing

y <- c(1,2,3,NA)
is.na(y) # returns a vector (F F F T)

 

x <- c(1,2,NA,3)

mean(x) # returns NA
mean(x, na.rm=TRUE) # returns 2

 

The function na.omit() returns the object with listwise deletion of missing values.

# create new dataset without missing data

newdata <- na.omit(mydata)

 

They are not supposed to give the same result. Consider this example:

exdf<-data.frame(a=c(1,NA,5),b=c(3,2,2)) # a b #1 1 3 #2 NA 2 #3 5 2 colMeans(exdf,na.rm=TRUE) ## remove only "NA" # a b #3.000000 2.333333 colMeans(na.omit(exdf)) ## remove "NA 2" # a b #3.0 2.5

Why is this? In the first case, the mean of column b is calculated through (3+2+2)/3. In the second case, the second row is removed in its entirety (also the value of b which is not-NA and therefore considered in the first case) by na.omit and so the b mean is just (3+2)/2.

 

  • REF:
  • http://www.cookbook-r.com/Basics/Working_with_NULL_NA_and_NaN/
  • http://stackoverflow.com/questions/7031127/data-frames-and-is-nan
  • http://www.r-bloggers.com/difference-between-na-and-nan-in-r/
  • http://help.scilab.org/docs/5.5.2/en_US/isnan.html
  • http://www.quantlego.com/howto/special-missing-values-in-r/

转载地址:http://hgdhl.baihongyu.com/

你可能感兴趣的文章
史上最全最常用的正则表达式
查看>>
选择只有一个孩子的元素
查看>>
欠薪不还,怎么办
查看>>
SVG 开发学习 总览
查看>>
spring读取properties文件的两种方式
查看>>
MYSQL 问题汇总
查看>>
How to customise the search box in Drupal 6
查看>>
[Android] Android 监听WIFI
查看>>
android Git使用gitignore建立项目过滤规则
查看>>
java对象--子父间的成员变量关系
查看>>
ECMAScript with 语句
查看>>
input输入框记录快捷键组合
查看>>
oracle积累
查看>>
JavaBean的另一种写法及其ruby版代码生成器
查看>>
Tsar 淘宝内部大量使用的系统监控工具
查看>>
MJRefresh 的运用 上拉刷新和下拉刷新
查看>>
一直想写点什么
查看>>
Io 语言入门
查看>>
Mysql常用命令
查看>>
maven servlet上传文件
查看>>