【问题标题】:Why my function fails to give correct outputs [duplicate]为什么我的函数无法提供正确的输出[重复]
【发布时间】:2020-09-20 13:32:24
【问题描述】:

我想出了一个非常简单的函数:

done <- function(x){
    x[x>1] <- NA
}

为了测试它,我创建了一个变量:

test <- 0:10

并执行

done(test)

但它输出的是原始测试数据,没有给出任何NA

【问题讨论】:

  • 在你的函数中添加return(x)

标签: r


【解决方案1】:

你还没有定义一个返回对象:

done <- function(x){
  x[x>1] <- NA
  return(x)
}

test <- 0:10

done(test)

另一种方法:

ifelse(test > 1, NA_integer_, test)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-24
    • 2021-10-06
    • 2018-10-28
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    相关资源
    最近更新 更多