R if语句在数据不可用时返回NULL

时间:2018-04-05 13:28:39

标签: r if-statement null

我正在尝试创建一个if语句,以便在filter(OD > threshold_1)%>%中找不到df但在library(dplyr) find_time = function(df, threshold_1, threshold_2, ODf){ return_value_1 = df %>% arrange(time) %>% filter(OD > threshold_1) %>% slice_(1) colnames(return_value_1)[1] <- "time_hdt_upper" colnames(return_value_1)[2] <- "OD_hdt_upper" return(data.frame(return_value_1)) } 中不存在时,它返回“NULL”而不是当前返回的内容......

[1] time_hdt_upper OD_hdt_upper  
<0 rows> (or 0-length row.names

返回:

NULL

因此,如果return_value_1不可用,我希望它返回time_hdt_upper OD_hdt_upper NULL NULL

#if return_value_1[1,1] equals NA set return_value_1[1,1] and return_value_1[1,2] to "NULL"

换句话说,我想: find_time = function(df, threshold_1){ return_value_1 = df %>% arrange(time) %>% filter(OD > threshold_1) %>% slice_(1) colnames(return_value_1)[1] <- "time_hdt_upper" colnames(return_value_1)[2] <- "OD_hdt_upper" if(OD %>% threshold_1 %in% df) {return("no threshold")} return(data.frame(return_value_1)) }

我尝试了以下组合:

Error in eval(lhs, parent, parent) : object 'OD' not found
Called from: eval(lhs, parent, parent)

返回错误:

$ jq '."environment"."tenant_id"=""' file
{
  "environment": {
    "cf_org": "EMPTY",
    "cf_shared_space": "EMPTY",
    "cf_user_name": "EMPTY",
    "cf_home_dir": "EMPTY",
    "tenant_id": ""
  }
}

1 个答案:

答案 0 :(得分:0)

让你的if语句看return_value

if (nrow(return_value) == 0) {
    return (NULL)
} else {
    return(return_value)
}