iterate over files and delete a record

I have a few files in a folder and their column headers are all the same.

I just want to go into each one and delete one record. It's a mystery record that causes everything to screw up.

Anyhow, I want to filter for [Record] == 1 and the delete it and write over the file without opening each one and doing this manually.

I have the below code, I think it's the writing the output part that's causing me some headaches.

files = dir_ls(glob = "Book*") #where I have my files
  
  fX_delete = function(X){
    
    
    read_csv(X) %>% #reading the file
      filter(Record <> 1) %>%  #filtering out the mystery record
      write.csv( paste0(glue{files}&".csv")) #writing the output

  }
  
  map(files,fX_delete) # using map to iterate

the error message I got is

Error in X & ".csv" : 
  operations are possible only for numeric, logical or complex types 

any help is appreciated!

This is not valid syntax can you explain what are you trying to achieve with this? What would be the desired output?

yes, I'm just trying to paste over the file itself. I don't know how to write over the actual file.

if R reads in Book_A.csv, I want to apply function and write over the same file as Book_A.csv again... I hope that makes sense.

fX_delete = function(X){
    
    
    read_csv(X) %>% #reading the file
      filter(Record <> 1) %>%  #filtering out the mystery record
      write.csv(X) #writing the output

  }

If the file name is the same then use your X variable again.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.