Home > Software engineering >  data.table fread, how to read in csv file last N rows
data.table fread, how to read in csv file last N rows

Time:07-02

Hі! Can someone show on the example of iris.csv how to load the last 10 rows with the fread function from the data.table thanks

udp=====

write.csv(iris,"C:\\Users\\TARAS\\Desktop\\iris.csv")
data.table::fread(cmd="tail -10 C:/Users/TARAS/Desktop/iris.csv")

"tail" ­Ґ пў«пҐвбп ў­гв७­Ґ© Ё«Ё ў­Ґи­Ґ©
Є®¬ ­¤®©, ЁбЇ®«­пҐ¬®© Їа®Ја ¬¬®© Ё«Ё Ї ЄҐв­л¬ д ©«®¬.
Null data.table (0 rows and 0 cols)

read.csv work fine

ir <- read.csv("C:\\Users\\TARAS\\Desktop\\iris.csv")
> head(ir)
  X Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 1          5.1         3.5          1.4         0.2  setosa
2 2          4.9         3.0          1.4         0.2  setosa
3 3          4.7         3.2          1.3         0.2  setosa
4 4          4.6         3.1          1.5         0.2  setosa
5 5          5.0         3.6          1.4         0.2  setosa
6 6          5.4         3.9          1.7         0.4  setosa

upd2=====

fpath = "C:/Users/TARAS/Desktop/iris.csv"
 fread(cmd=paste0("tail -10 '", fpath, "'"))

also i tried \ , \\ , / , // ,

"tail" ­Ґ пў«пҐвбп ў­гв७­Ґ© Ё«Ё ў­Ґи­Ґ©
Є®¬ ­¤®©, ЁбЇ®«­пҐ¬®© Їа®Ја ¬¬®© Ё«Ё Ї ЄҐв­л¬ д ©«®¬.



Null data.table (0 rows and 0 cols)
Warning messages:
1: In (if (.Platform$OS.type == "unix") system else shell)(paste0("(",  :
  '(tail -10 'C:/Users/TARAS/Desktop/iris.csv') > C:\Users\TARAS\AppData\Local\Temp\RtmpcDiW2w\file108833c6463' execution failed with error code 1
2: In fread(cmd = paste0("tail -10 '", fpath, "'")) :
  File 'C:\Users\TARAS\AppData\Local\Temp\RtmpcDiW2w\file108833c6463' has size 0. Returning a NULL data.table.

CodePudding user response:

You can have fread read the last n rows of a .csv using cmd param

fread(cmd="tail -10 iris.csv")

If you want the header information also, you can wrap the above in a call to data.table::setnames, where the names are obtained by reading the file without any rows (nrows=0):

setnames(
  fread(cmd="tail -10 iris.csv"),
  names(fread("iris.csv",nrows = 0))
)

If you have full path, you can do this:

fpath = "C:/Users/xxxx/Desktop/folder 1/iris.csv"

setnames(
  fread(cmd=paste0("tail -10 '", fpath, "'")),
  names(fread(fpath,nrows = 0))
)[]
  • Related