Get the number of lines in a text file using R

后端 未结 5 1261
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 10:51

Is there a way to get the number of lines in a file without importing it?

So far this is what I am doing

myfiles <- list.files(pattern=\"*.dat\")
         


        
5条回答
  •  心在旅途
    2020-12-05 11:16

    If you are using linux, this might work for you:

    # total lines on a file through system call to wc, and filtering with awk
    target_file   <- "your_file_name_here"
    total_records <- as.integer(system2("wc",
                                        args = c("-l",
                                                 target_file,
                                                 " | awk '{print $1}'"),
                                        stdout = TRUE))
    

    in your case:

    #
    lapply(myfiles, function(x){
                             as.integer(system2("wc",
                                                args = c("-l",
                                                         x,
                                                         " | awk '{print $1}'"),
                                                stdout = TRUE))
                          }
                      )
    

提交回复
热议问题