前言:
学习R过程中,数据中往往会出现一些诡异的符号,而不是数值,那么他们到底是什么意思呢?下面带领大家一探究竟。
(一) 特殊值的概念
1. NaN
R中的无定义数用NaN表示,即“Not a Number(非数)”。
不过在R中,R实际上是把NaN视作一个数的,当其参与运算时,返回结果总是NaN。我们可以使用is.nan()函数来检测计算结果有无定义,但是需要注意的是,对于NaN而言,is.finite()和is.infinite()都会返回FALSE。
> 0/0
[1] NaN
2. NA
NA表示缺失值,即“Missing value”,是“not available”的缩写
> a <- c(1, 2, 3, 4)
> a[1:5]
[1] 1 2 3 4 NA
3. Inf
R中的无穷大用Inf表示(即Infinity,无穷大),负无穷表示为-Inf。
要检查一个数是否为无穷,可以使用is.finite()或者is.infinite()函数
> 1/0
[1] Inf
> -1/0
[1] -Inf
3. NULL
R语言中,NA代表位置上的值为空,NULL代表连位置都没有,变量为空,其长度为0,表明“空无一物”
> a <- NULL
> a
NULL
> length(a)
[1] 0
(二) 实战中处理无效数据
#设置sugar数据
> sugar <- matrix(1:25,5,5)
> sugar[1,] <- 0
> sugar[3,1] <- 0
> sugar
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 2 7 12 17 22
[3,] 0 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
#设置yeast数据
> yeast <- matrix(10:34,,5,5)
> yeast[3,] <- 0
> yeast[1,1] <- 0
> yeast
[,1] [,2] [,3] [,4] [,5]
[1,] 0 11 12 13 14
[2,] 15 16 17 18 19
[3,] 0 0 0 0 0
[4,] 25 26 27 28 29
[5,] 30 31 32 33 34
将两个数据进行除法运算
> foldchange <- sugar/yeast
> foldchange
[,1] [,2] [,3] [,4] [,5]
[1,] NaN 0.0000000 0.0000000 0.0000000 0.0000000
[2,] 0.1333333 0.4375000 0.7058824 0.9444444 1.1578947
[3,] NaN Inf Inf Inf Inf
[4,] 0.1600000 0.3461538 0.5185185 0.6785714 0.8275862
[5,] 0.1666667 0.3225806 0.4687500 0.6060606 0.7352941
> log2_foldchange <- log2(sugar / yeast)
> log2_foldchange
[,1] [,2] [,3] [,4] [,5]
[1,] NaN -Inf -Inf -Inf -Inf
[2,] -2.906891 -1.192645 -0.5025003 -0.08246216 0.2115041
[3,] NaN Inf Inf Inf Inf
[4,] -2.643856 -1.530515 -0.9475326 -0.55942741 -0.2730185
[5,] -2.584963 -1.632268 -1.0931094 -0.72246602 -0.4436067
而log2_foldchange就是我们需要的数据,发现里面有许多 NaN、 -Inf 、Inf ,想办法进行数据清洗。
> yeast == 0 # 逻辑判断数据中是否为0
[,1] [,2] [,3] [,4] [,5]
[1,] TRUE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE
[3,] TRUE TRUE TRUE TRUE TRUE
[4,] FALSE FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE FALSE
> log2_foldchange[yeast == 0] # 选择里面判断为0的数据
[1] NaN NaN Inf Inf Inf Inf
# 将无效值替换为0
> log2_foldchange[sugar == 0 | yeast == 0] <- 0
> log2_foldchange
[,1] [,2] [,3] [,4] [,5]
[1,] 0.000000 0.000000 0.0000000 0.00000000 0.0000000
[2,] -2.906891 -1.192645 -0.5025003 -0.08246216 0.2115041
[3,] 0.000000 0.000000 0.0000000 0.00000000 0.0000000
[4,] -2.643856 -1.530515 -0.9475326 -0.55942741 -0.2730185
[5,] -2.584963 -1.632268 -1.0931094 -0.72246602 -0.4436067
都到这一步了,再绘制两个图形玩玩:
- hist图
> hist(log2_foldchange, col = "red", border = "black")
- 密度图
> curve <- density(log2_foldchange)
> plot(curve, main = "understand the NaN Inf NA")
> polygon(curve, col = "Thistle", border = "red", lty =1 )