如何在R中阅读数据时包含条件语句?

fykwrbwg  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(203)

使用下面提到的R代码,我正在阅读保存在不同文件夹中的数据(.txt文件)。我需要在代码中包含以下条件。
我的txt文件的结构如下:

Date/Time XY [XY] C1 [m2] C1c C2 [m] C2c C3 [W] C3c K PP [Pa]..
2005-03-01S01:00:00 0.98 250 0 29 0 289 0 98 289...
2005-03-01S02:00:00 0.97 240 0 28 2 279 0 98 89...
2005-03-01S03:00:00 0.98 252 -1 29 0 289 0 16 289...
..
..

我希望代码中包含以下条件。

if C1c is not = 0, then C1 = NA,
   if -400 > C1 > 350, then C1 = NA,
   if C2c is not = 0, then C2 = NA,
   if -250 > C2 > 450, then C2 = NA, 
   if C3c is not = 0, then C3 = NA,
   if 100 > C3 > 500, then C3 = NA,
   if K < 90, then K = NA
   if PP < 200, then PP = NA

要注意的是,并非所有文本文件都具有所有这些列。因此,逻辑应该是,如果文件具有相关列,则应将相应的条件应用于该列。
现有代码:

library(data.table)

filelist <- list.files("D:/Test2/", full.names = TRUE, recursive 
                   = TRUE, pattern = ".txt$")
dt <- lapply(filelist, function(file) {
  lines <- readLines(file)
  comment_end = match("*/", lines)
  fread(file, skip = comment_end)
})

dt.tidied <- lapply(dt, FUN = function(x){
  setnames(x, old = "T2 [?C]", new = "T2 [°C]", skip_absent = TRUE)
  colnames(x) <- gsub("\\[", "(", colnames(x))
  colnames(x) <- gsub("\\]", ")", colnames(x))

  return(x)
})

merged <- rbindlist(dt.tidied, fill = TRUE, use.names = TRUE)

write.csv(merged, "D:/Test2/Merged2.csv")

任何人都可以请帮助我修改代码,包括条件。

juzqafwq

juzqafwq1#

包括逻辑以在依赖于该列的任何操作之前测试该列是否存在,例如:

dt.tidied <- lapply(dt, FUN = function(x){
  setnames(x, old = "T2 [?C]", new = "T2 [°C]", skip_absent = TRUE)
  colnames(x) <- gsub("\\[", "(", colnames(x))
  colnames(x) <- gsub("\\]", ")", colnames(x))
  
  # Apply conditions to the respective columns
  if ("C1c" %in% colnames(x)) {
    x[C1c != 0, C1 := NA]
    x[C1 < -400 | C1 > 350, C1 := NA]
  }
  
  if ("C2c" %in% colnames(x)) {
    x[C2c != 0, C2 := NA]
    x[C2 < -250 | C2 > 450, C2 := NA]
  }
  
  if ("C3c" %in% colnames(x)) {
    x[C3c != 0, C3 := NA]
    x[C3 < 100 | C3 > 500, C3 := NA]
  }
  
  if ("K" %in% colnames(x)) {
    x[K < 90, K := NA]
  }
  
  if ("PP" %in% colnames(x)) {
    x[PP < 200, PP := NA]
  }
  
  return(x)
})

相关问题