assign headers based on existing row in dataframe in R

后端 未结 5 602
谎友^
谎友^ 2020-12-12 19:31

After transforming a dataframe, I would like to assign heads/names to the columns based on an existing row. My headers are currently:

相关标签:
5条回答
  • 2020-12-12 19:37

    A new answer that uses dplyr and tidyr:

    Extracts the desired column names and converts to a list

    library(tidyverse)
    
    col_names <- raw_dta %>% 
      slice(2) %>%
      pivot_longer(
        cols = "X2":"X10", # until last named column
        names_to = "old_names",
        values_to = "new_names") %>% 
      pull(new_names)
    

    Removes the incorrect rows and adds the correct column names

    dta <- raw_dta %>% 
      slice(-1, -2) %>% # Removes the rows containing new and original names
      set_names(., nm = col_names)
    
    
    0 讨论(0)
  • 2020-12-12 19:42

    The cleanest way is use a function of janitor package that is built for exactly this purpose.

    janitor::row_to_names(DF,1)
    

    If you want to use any other row than the first one, pass it in the second parameter.

    0 讨论(0)
  • 2020-12-12 19:43

    Try this:

    colnames(DF) = DF[1, ] # the first row will be the header
    DF = DF[-1, ]          # removing the first row.
    

    However, get a look if the data has been properly read. If you data.frame has numeric variables but the first row were characters, all the data has been read as character. To avoid this problem, it's better to save the data and read again with header=TRUE as you suggest. You can also get a look to this question: Reading a CSV file organized horizontally.

    0 讨论(0)
  • 2020-12-12 19:44

    Very similar to Vishnu's answer but uses the lapply to map all the data to characters then to assign them as the headers. This is really helpful if your data is imported as factors.

    DF[] <- lapply(DF, as.character)
    colnames(DF) <- DF[1, ]
    DF <- DF[-1 ,]
    

    note that that if you have a lot of numeric data or factors you want you'll need to convert them back. In this case it may make sense to store the character data frame, extract the row you want, and then apply it to the original data frame

    tempDF <- DF
    tempDF[] <- lapply(DF, as.character)
    colnames(DF) <- tempDF[1, ]
    DF <- DF[-1 ,]
    tempDF <- NULL
    
    0 讨论(0)
  • 2020-12-12 20:00

    The key here is to unlist the row first.

    colnames(DF) <- as.character(unlist(DF[1,]))
    DF = DF[-1, ]
    
    0 讨论(0)
提交回复
热议问题