use first row data as column names in r

I have a dirty dataset that I could not read it with header = T. After I read and clean it, I would like to use the now first row data as the column name. I tried multiple methods on Stack Overflow without success. What could be the problem?

The dataset t1 should look like this after clean up:

      V1    V2  V3  V4  V5
1   col1    col2    col3    col4
2   row1    2   4   5   56
3   row2    74  74  3   534
4   row3    865 768 8   7
5   row4    68  86  65  87
  • I tried: colnames(t1) <- t1[1,]. Nothing happens.
  • I tried: names(t1) <- ti[1,], Nothing happens.
  • I tried: lapply(t1, function(x) {names(x) <- x[1, ]; x}). It returns an error message: Error in `[.default`(x, 1, ) : incorrect number of dimensions

Could anyone help?

Leave a Comment