use first row data as column names in r

后端 未结 8 1611
遇见更好的自我
遇见更好的自我 2020-12-05 09:58

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 tri

8条回答
  •  失恋的感觉
    2020-12-05 10:44

    header.true <- function(df) {
      names(df) <- as.character(unlist(df[1,]))
      df[-1,]
    }
    

    Test

    df1 <- data.frame(c("a", 1,2,3), c("b", 4,5,6))
    header.true(df1)
      a b
    2 1 4
    3 2 5
    4 3 6
    

提交回复
热议问题