+4 votes
in Programming Languages by (9.6k 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

+1 vote
by (11.1k points)
edited by

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
+5 votes
1 answer
+2 votes
1 answer
+5 votes
1 answer
asked Sep 16, 2021 in Programming Languages by praveen (18.0k points)