use first row data as column names in r

拟墨画扇 提交于 2019-12-02 23:28:21
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

Take a step back, when you read your data use skip=1 in read.table to miss out the first line entirely. This should make life a bit easier when you're cleaning data, particularly for data type. This is key as your problem stems from your data being encoded as factor.

You can then read in your column names separately with nrows=1 in read.table.

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
    

    How about:

    my.names <- t1[1,]
    
    colnames(t1) <- my.names
    

    i.e. specifically naming the row as a variable?

    with the following code:

    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.frame(namex, row1, row2, row3, row4)
    t1 <- t(t1)
    
    my.names <- t1[1,]
    
    colnames(t1) <- my.names
    

    It seems to work, but maybe I'm missing something?

    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,]
    

    Similar to some of the other answers, here is a dplyr/tidyverse option:

    library(tidyverse)
    
    names(df) <- df %>% slice(1) %>% unlist()
    df <- df %>% slice(-1)
    
    标签
    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!