0

I have data with various columns for say A, B, C, Cq, D, Dq, F, Fq, M, Mq, ..., X. I want that whenever column X has value > 200, that particular row for all the columns should be removed. My code in R is as below. Could anyone please help me in modifying the code accordingly. Also, I am not sure if I should put this condition in col_names or on merged?

col_names <- function(dt_list){
  lapply(dt_list, FUN = function(x){

    if ("ABq" %in% colnames(x)) {
      x[ABq != 0, AB := NA]
      x[AB < 10 | DIF > 100, AB := NA]
    }

    if ("MNq" %in% colnames(x)) {
      x[MNq != 0, MN := NA]
      x[MN < 25 | MN > 150, MN := NA]
    }

    return(x)
  })
}

for (idx in seq_along(dirlist)){
  filelist <- list.files(path = dirlist[idx], full.names = TRUE, recursive = TRUE, pattern = 
                       ".txt$")
  dt_ <- read_the_files(filelist)
  dt.tidied <- col_names(dt_)

  #bind, filling missing columns to NA
  merged <- rbindlist(dt.tidied, fill = TRUE, use.names = TRUE)

Update: dt_ is a large list, not a dataframe.

  • You say you have a data, why not post part of it?The function is your thoughts. You should consider adding the data and the expected output. Why should we modify the code if it is correct? If not how will we do it unless we write our own code? – Onyambu Jun 26 '23 at 16:23

1 Answers1

0

I believe the solution is quite simple:

Assuming dt_is the name of your data frame.

dt_ <- dt_[dt_$X<200,]

Voilá

JMenezes
  • 1,004
  • 1
  • 6
  • 13
  • It is giving me an error Error in dt_[dt_$X < 200, ] : incorrect number of dimensions. How can I resolve it? – Michael_Brun Jun 30 '23 at 08:45
  • I am not sure but the error could be because dt_ is not a dataframe but a large list. – Michael_Brun Jun 30 '23 at 09:21
  • I see. I think your statement that your data has columns led me to assume it was a data.frame. Is it correct to assume that your list has elements of equal length? IF so I think as.data.frame will convert it to a data.frame and my code above should work – JMenezes Jun 30 '23 at 15:31