R中一个函数中使用了”If”、”else”和”is.na()”。

huangapple go评论64阅读模式
英文:

The usage of "If", "else", "is.na()" in one function of R

问题

我有一个数据框中的LengthWeight数值。然而,其中一些数值是缺失的。数据框如下:

df <- data.frame(
  L = c(13,15,19,NA,NA,32,35,NA,NA,18,15),
  W = c(NA,NA,50, NA,NA,NA,80,NA,NA,30,NA)
)

我需要一个函数,当长度不是NA而重量是NA时可以计算长度对应的重量,否则不执行任何操作。

lwr <- function(data, length, weight, a, b) {
  if(!is.na(data$length) && is.na(data$weight)) { 
    data$weight = 10^(log(a) + b*log(data$length))
  } else {
    data$weight
  }
}

在这里使用函数:

lwr(data=df, length = L, weight = W, a=0.003, b=3.2)

但它似乎不起作用。

如果你能帮助我,我将不胜感激。非常感谢您的时间。

英文:

I have Length and Weight values in a data frame. However some of them are missing. The data frame is like:

df &lt;- data.frame(
  L = c(13,15,19,NA,NA,32,35,NA,NA,18,15),
  W = c(NA,NA,50, NA,NA,NA,80,NA,NA,30,NA)
)

I need a function which will work when length is not NA and weight is NA. it will calculate the weight for length, and else it will do nothing.

lwr &lt;- function(data, length, weight, a, b) {
  if(!is.na(data$length) &amp;&amp; is.na(data$weight)) { 
    data$weight = 10^(log(a) + b*log(data$length))
  } else {
    data$weight
  }
}

Here we go

lwr(data=df, length = L, weight = W, a=0.003, b=3.2)

but it does not work.

If you help me, I would be appreciated. Thank you very much for your time.

答案1

得分: 1

你应该使用向量化的 ifelse(),而不是 if()...else...

lwr <- function(length, weight, a, b) {
  ifelse(is.na(weight), 10^(log(a) + b*log(length)), weight)
}

df %>%
  transform(W2 = lwr(L, W, a=0.003, b=3.2))

# 等价于:
# df$W2 <- lwr(df$L, df$W, a=0.003, b=3.2)
#     L  W          W2
# 1  13 NA    250.4350
# 2  15 NA    718.8159
# 3  19 50     50.0000
# 4  NA NA          NA
# 5  NA NA          NA
# 6  32 NA 191078.5331
# 7  35 80     80.0000
# 8  NA NA          NA
# 9  NA NA          NA
# 10 18 30     30.0000
# 11 15 NA    718.8159
英文:

You should use the vectorized ifelse(), instead of if()...else....

lwr &lt;- function(length, weight, a, b) {
  ifelse(is.na(weight), 10^(log(a) + b*log(length)), weight)
}

df |&gt;
  transform(W2 = lwr(L, W, a=0.003, b=3.2))

# equivalent:
# df$W2 &lt;- lwr(df$L, df$W, a=0.003, b=3.2)
#     L  W          W2
# 1  13 NA    250.4350
# 2  15 NA    718.8159
# 3  19 50     50.0000
# 4  NA NA          NA
# 5  NA NA          NA
# 6  32 NA 191078.5331
# 7  35 80     80.0000
# 8  NA NA          NA
# 9  NA NA          NA
# 10 18 30     30.0000
# 11 15 NA    718.8159

答案2

得分: 1

你可能可以更轻松地做到这一点。

f <- \(x, a, b) 10^(log(a) + b*log(x))

naw <- is.na(df$W)

df$W[naw] <- f(df$L[naw], .003, 3.2)
#     L           W
# 1  13    250.4350
# 2  15    718.8159
# 3  19     50.0000
# 4  NA          NA
# 5  NA          NA
# 6  32 191078.5331
# 7  35     80.0000
# 8  NA          NA
# 9  NA          NA
# 10 18     30.0000
# 11 15    718.8159
英文:

You probably could do that easier.

f &lt;- \(x, a, b) 10^(log(a) + b*log(x))

naw &lt;- is.na(df$W)

df$W[naw] &lt;- f(df$L[naw], .003, 3.2)
#     L           W
# 1  13    250.4350
# 2  15    718.8159
# 3  19     50.0000
# 4  NA          NA
# 5  NA          NA
# 6  32 191078.5331
# 7  35     80.0000
# 8  NA          NA
# 9  NA          NA
# 10 18     30.0000
# 11 15    718.8159

huangapple
  • 本文由 发表于 2023年2月18日 16:00:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定