Reading a file using fread with row and column names

Using fread, how to read a CSV file containing row and column names. I tried following, but it does not read row and column names correctly.

The csv file looks like this: (where C1, C2, C3 are column names, and r1, r2, r3 are row names)

input = ",C1,C2,C3 r1,A,B,C r2,1,3,5 3,2,4,6" 

I am using the function

 require(data.table) fread(input,header = TRUE) 

which give

  r1 ABC 1: r2 1 3 5 2: 3 2 4 6 

How can I read CSV correctly using fread?

+6
source share
1 answer

You must submit a bug report.

Here is the work:

 colnames <- strsplit(readLines(textConnection(input), n=1), ",")[[1]] colnames[1] <- "rownames" setnames(DT <- fread(input, skip=1, header=FALSE), colnames) DT # rownames C1 C2 C3 #1: r1 ABC #2: r2 1 3 5 #3: 3 2 4 6 

As you know, data.table does not support growth names.

+4
source

Source: https://habr.com/ru/post/973488/


All Articles