+4 votes
in Programming Languages by (40.5k points)

I am reading a TSV file using fread() wither header=FALSE. The file does not have a header, so  fread() adds V1, V2,... as header.

I have the list of columns. How can I add those columns to the data table as the header?

1 Answer

+2 votes
by (60.0k points)
selected by
 
Best answer

R function colnames() can be used to retrieve or set column names of a matrix-like object.

First, read the TSV file using the fread() function with header=FALSE, then use colnames() to add the header to your data table.

Here is an example:

variable "cols" contains column names.

E.g. cols <- c("a1", "b1", "c1")

v <- fread(filename, sep="\t", header=FALSE)

colnames(v) <- cols

Related questions

+5 votes
1 answer
+2 votes
1 answer
+5 votes
1 answer
asked Jul 27, 2021 in Programming Languages by kush (40.5k points)

...