use first row data as column names in r

后端 未结 8 1591
遇见更好的自我
遇见更好的自我 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
    
    0 讨论(0)
  • 2020-12-05 10:46

    Probably, the data type of the data frame columns are factors. That is why the code you tried didn't work, you can check it using str(df):

  • First option
  • Use the argument stringsAsFactors = FALSEwhen you import your data:

    df <- read.table(text =  "V1    V2  V3  V4  V5
                            col1    col2    col3    col4 col5
                            row1    2   4   5   56
                            row2    74  74  3   534
                            row3    865 768 8   7
                            row4    68  86  65  87", header = TRUE, 
                            stringsAsFactors = FALSE )
    

    Then you can use your first attempt, then remove your first row if you'd like:

    colnames(df) <- df[1,]
    df <- df[-1, ] 
    

  • Second option
  • It will work if your columns are factors or characters:

    names(df) <- lapply(df[1, ], as.character)
    df <- df[-1,] 
    

    Output:

      col1 col2 col3 col4 col5
    2 row1    2    4    5   56
    3 row2   74   74    3  534
    4 row3  865  768    8    7
    5 row4   68   86   65   87
    
0 讨论(0)
提交回复
热议问题