rdataframelistr-rownames

First row as column names in a list of data frames(2)


I want to rename the columns of every data frame in my list of data frames with the first row.

I tried the code from this question First row as column names in a list of data frames but it returns first_row_name=rows_number /c(date=3)/

dflist1 <- lapply(dflist, function(x){
  names(x) <- x[1,]
  x <- x[-1,]
  return(x)
})

Solution

  • The issue is because the columns were factor. So, we unlist and convert to character class

    names(x) <- as.character(unlist(x[1,]))