简体   繁体   English

在R中从另一个减去一个数字时出现NA

[英]NA's appearing when subtracting one number from another in R

I have data in the following format in R: 我在R中具有以下格式的数据:

Col1 Col2 
1    1
2    1
4    0
0    0
2    2
.    .
.    .
.    .

I'm using the following script to work out the percentage differences between Col1 & Col2 in each row. 我正在使用以下脚本来计算每一行中Col1和Col2之间的百分比差异。

temp <- matrix(numeric(),dim(data)[1],1)

for (i in 1:dim(data)[1])
{
 temp[i,1]<- ((data[i,1]-data[i,2)/data[i,1])*100
}

For some reason my output file (temp) has some NA's in it. 由于某种原因,我的输出文件(temp)中包含一些NA。 They are occurring even when 0-0. 即使在0-0时,它们也会发生。 Does anyone know why it isn't just producing a 0 as opposed to NA? 有谁知道为什么它不只是产生0而不是NA? Some sums of 0-0 are producing a 0 as opposed to NA so I can't see any real pattern. 一些0-0的总和相对于NA产生一个0,所以我看不到任何真实的模式。

Any help would be much appreciated. 任何帮助将非常感激。 Thanks, 谢谢,

You are making the classic division by zero error. 您正在将经典除以零误差。 R reports this as NaN - not a number, which is correct. R将其报告为NaN而不是数字,这是正确的。

x <-structure(list(
  Col1 = c(1L, 2L, 4L, 0L, 2L), Col2 = c(1L, 1L, 0L, 0L, 2L)), 
  .Names = c("Col1", "Col2"), class = "data.frame", row.names = c(NA, -5L))

with(x, (Col2-Col1)/Col1)
[1]  0.0 -0.5 -1.0  NaN  0.0

One way to work around this is to use ifelse to return zero whenever Col==0 : 解决此问题的一种方法是,每当Col==0时,使用ifelse返回零:

with(x, ifelse(Col1==0, 0, (Col2-Col1)/Col1))
[1]  0.0 -0.5 -1.0  0.0  0.0

If you don't want to use with , then write your code like this (more verbose but identical): 如果你不希望使用with ,然后再编写这样的代码(更详细的,但相同的):

ifelse(x$Col1==0, 0, (x$Col2-x$Col1)/x$Col1)
[1]  0.0 -0.5 -1.0  0.0  0.0

For those entries, you're dividing by zero: 对于这些条目,您要除以零:

(0 - 0) / 0

Mathematically, the result of such a division is not defined . 从数学上讲,这种除法的结果未定义 To indicate this, R evaluates 0/0 as NaN . 为了表明这一点,R将0/0评估为NaN

Here is a concise way to compute all percentags at once, returning zeroes for entries where both Col1 and Col2 are zero: 这是一次计算所有百分比的简洁方法,对于Col1Col2均为零的条目,返回零。

> data <- data.frame(Col1=c(1,2,4,0,2), Col2=c(1,1,0,0,2))
> pmax(100.0*(data$Col1-data$Col2)/data$Col1, 0, na.rm=T)
[1]   0  50 100   0   0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM