3

I am a new R user. I want to perform the same set of multiple tasks for many files and write the results into text files. The code looks like this:

    ps_files<-dir(pattern = ".ps")
    lapply(ps_files, function(x) {
    #read files
    read<-data.table::fread(x, data.table = F, stringsAsFactors = F) 
    #merge with bim file
    merge_bim<-dplyr::inner_join(read, bim_df[,c(1:2, 4:6)], by=c("V1"="V2"))
    #paste Chr for rows in column1
    merge_bim$V1.y<-paste0("Chr",merge_bim$V1.y)
    #filter significant snps
    sig_snps<-filter(merge_bim, V4.x<=0.00001)
    #get columns needed by annovar
    output<-sig_snps[,c("V1.y","V4.y","V4.y","V5","V6")]
    #create text files of results
    write.table(output, sep="\t", col.names=F, row.names=F, append=F, quote=F)
    })

The directory has five files in it. When I try to run until the output variable, my expected results come out. But when I try to run until the write.table, I get "NULL" for each file (i. e. [[1]] NULL...[[5]] NULL), and text files are not produced. I tried suggestions from multiple websites but the error persists. I'm also not sure if lapply is the most appropriate function to use.

M--
  • 25,431
  • 8
  • 61
  • 93
lala
  • 35
  • 5

0 Answers0