以R data.frame中的值为条件获取日志

时间:2014-04-03 12:24:21

标签: r

我有一些数据,我需要记录它们。但是我有两个NA值和0.默认情况下,R使log(0)= NA,但我需要区分0和NA。因此,我试图告诉R只计算不同于零的值的日志。我尝试了几个规格,但都失败了。 x是维度N x M的data.frame,列是变量,行是观察。像这样的东西

x <- cbind(c(3,4,0,0), c(0,5,NA,6),c(0,2,NA,NA))
x <- data.frame(x)
x
     [,1] [,2] [,3]
[1,]    3    0    0
[2,]    4    5    2
[3,]    0   NA   NA
[4,]    0    6   NA

我的最后一次试验是

x_log <- matrix(NA, dim(x)[1], dim(x)[2])
for (i in 1:dim(x)[1]){
if (x[i,] == 0) { x_log[i,] <- x[i,]}
 else {x_log[i,] <- log(x[i,])}
}

我收到以下错误和警告

Error in x_log[i, ] <- log(x[i, ]) : 
incorrect number of subscripts on matrix
In addition: Warning messages:
1: In if (x[i, ] == 0) { :
the condition has length > 1 and only the first element will be used
2: In if (x[i, ] == 0) { :
the condition has length > 1 and only the first element will be used

我也试过这个

x_log <- matrix(NA, dim(x)[1], dim(x)[2])
for (i in 1:dim(x)[1])
for (j in 1:dim(x)[2])
{ if (x[i,j] == 0) { x_log[i,j] <- x[i,j]}
 else {x_log[i,j] <- log(x[i,j])}
}

并获取

Error in if (x[i, j] == 0) { : 
missing value where TRUE/FALSE needed

我哪里错了?还有另一种更有效的方法来做我想做的事吗?

2 个答案:

答案 0 :(得分:1)

x <- cbind(c(3,4,0,0), c(0,5,NA,6),c(0,2,NA,NA)) # Your data and output
x <- data.frame(x)
x[!is.na(x) & x > 0] <- log(x[!is.na(x) & x > 0]) # My solution
#        X1       X2        X3
#1 1.098612 0.000000 0.0000000
#2 1.386294 1.609438 0.6931472
#3 0.000000       NA        NA
#4 0.000000 1.791759        NA

答案 1 :(得分:0)

在R:log(0)==-Inf而不是NA

> x <- cbind(c(3,4,0,0), c(0,5,NA,6),c(0,2,NA,NA))
> apply(x,1:2,log)
         [,1]     [,2]      [,3]
[1,] 1.098612     -Inf      -Inf
[2,] 1.386294 1.609438 0.6931472
[3,]     -Inf       NA        NA
[4,]     -Inf 1.791759        NA