use first row data as column names in r

后端 未结 8 1615
遇见更好的自我
遇见更好的自我 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:22

    Using data.table:

    library(data.table)
    
    namex <-c("col1","col2","col3","col4")
    row1 <- c(2, 4, 5, 56)
    row2 <- c(74, 73, 3, 534)
    row3 <- c(865, 768, 8, 7)
    row4 <- c(68, 58, 65, 87)
    
    t1 <- data.table(namex, row1, row2, row3, row4)
    t1 <- data.table(t(t1))
    
    setnames(t1, as.character(t1[1,]))
    t1 <- t1[-1,]
    

提交回复
热议问题