简体   繁体   English

在if语句中使用is.na()和!is.na()

[英]Using is.na() and !is.na() in if statements

So I'm trying to create a loop which reads input files based on the value present in a column 4 of my data frame. 因此,我试图创建一个循环,该循环根据数据框第4列中的值读取输入文件。

    for (idx in 1:nrow(config_path_test)) {
         if (config_path_test[idx,2] == "ens84" & is.na(config_path_test[idx,4])) 
         {#process Raw data}

         else {
            if(config_path_test[idx,2] == "ens84" & !is.na(config_path_test[idx,4]))
              {#process TPM data}
              }

Tested my code with column 4 containing paths to my data in all fields, no paths in any of the fields or a combination of both (some have paths, some will not). 在第4列中测试了我的代码,其中第4列包含所有字段中数据的路径,任何字段中都没有路径或两者均组合(有些具有路径,有些则没有)。

Code above deals with both all and no fields succesfully. 上面的代码成功处理了所有字段和没有字段。 However, for the combination of both, I'm a bit stuck. 但是,对于两者的结合,我有点卡住了。

As no paths being present is reflected for the entire column as NA, i've been using: 由于没有路径反映为NA,因此整个列均被使用:

   is.na() and !is.na()

However, a combination of both will not show NA for the missing values. 但是,两者的组合不会显示缺少值的NA。 The fields are blank. 字段为空白。

Any idea how to change my code to process the blank fields? 任何想法如何更改我的代码以处理空白字段? Or any idea on how to process the blank values to NA? 或关于如何将空白值处理为NA的任何想法?

Thanks in advance 提前致谢

If I am understanding the issue correctly, the files you have read in have some ("") fields that you would like to cast to NA? 如果我正确地理解了该问题,那么您读入的文件中有一些要转换为NA的(“”)字段? If that is the case, do the following outside your function: 如果是这种情况,请在功能之外执行以下操作:

df[df == ""] <- NA

Do check to ensure it is not something similar, such as (" "). 请检查以确保它不是类似的东西,例如(“”)。 This thread might also be of interest to you. 您可能也对此线程感兴趣。

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

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