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

问题描述 投票:0回答:1

使用 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")

谁能帮我修改代码以包含条件。

r if-statement conditional-statements read.table r-colnames
1个回答
0
投票

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

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)
})
© www.soinside.com 2019 - 2024. All rights reserved.