use first row data as column names in r

make first row column names in r dplyr
first row to column names r dplyr
make first row into column names in r
colnames r
r get column name by index
change column name in r
r read.table first row column names
convert first row to column names in r

This should be such an easy problem but I have trouble with. 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 tried multiple methods on stackoverflow without success. What could be the problem? The dataset t1 should look like this after clean up:

      V1    V2  V3  V4  V5
1   col1    col2    col3    col4
2   row1    2   4   5   56
3   row2    74  74  3   534
4   row3    865 768 8   7
5   row4    68  86  65  87

I tried: colnames(t1)=t1[1,]. Nothing happens.

I tried: names(t1)=ti[1,], Nothing happens.

I tried: lapply(t1, function(x) {names(x)<-x[1,]; x})

it returns an error message:

Error in [.default(x, 1, ) : incorrect number of dimensions

Could anyone help?

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

Second row as column headers using DT datatable in R shiny , Second row as column headers using DT datatable in R shiny · General · dplyr Add the first row as names for the DF; delete the first row  R stores the row and column names in an attribute called dimnames. Use the dimnames() function to extract or set those values. Using names as indices. These row and column names can be used just like you use names for values in a vector. You can use these names instead of the index number to select values from a vector.

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.

Setting the first row as column name in R, Hi, I have a data matrix `mat` of 622 samples as columns and cell types as rows. sample an Differential analysis with replicates using edgeR. Hi,  If the object has dimnames the first component is used as the row names, and the second component (if any) is used for the column names. For a data frame, rownames and colnames eventually call row.names and names respectively, but the latter are preferred.

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
    

    colnames: Row and Column Names, If the object has dimnames the first component is used as the row names, and the second component (if any) is used for the column names. For a data frame,  I'm working with a data frame in r where my row names are meaningful. Hence, I would like to give the column of row names a name. How do I do this?

    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?

    Addressing Data – Programming with R, The first row of this csv file is a list of column names. We used the header = TRUE argument to read.csv so that R can interpret the file correctly. We are using the  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.

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

    as.data.frame function, Usage. as.data.frame(x, row.names = NULL, optional = FALSE, …) Note that all of R's base package as.data.frame() methods use optional only for column by '​flattening' all dimensions after the first and creating suitable column labels. Row names of the form 1:n for n > 2 are stored internally in a compact form, which might be seen from C code or by deparsing but never via row.names or attr(x, "row.names"). Additionally, some names of this sort are marked as ‘automatic’ and handled differently by as.matrix and data.matrix (and potentially other functions).

    8.4 Dataframe column names, An introductory book to R written by, and for, R pirates. You can use these name to access specific columns by name without having to know To do this, just use the df$name notation and assign a new vector of data to it. For example, let's change the name of the first column of survey from index to participant.number. Add a Column as Rownames. Takes an existing column and uses it as rownames instead. This is useful when turning a data.frame into a matrix. Inspired by the tibble package's column_to_row which is now deprecated if done on a tibble object. By coercing to a data.frame this problem is avoided.

    Data Input, Reads a file in table format and creates a data frame from it, with cases corresponding If there is a header and the first row contains one fewer field than the number of columns, the The default is to use "V" followed by the column number. In this tutorial, you will learn how to select or subset data frame columns by names and position using the R function select () and pull () [in dplyr package]. We’ll also show how to remove columns from a data frame. pull (): Extract column values as a vector. The column of interest can be specified either by name or by index.

    Dataframe has no column names. How to add a header?, In your code , can you remove header=0 ? This basically tells pandas to take the first row as the column headers . Once you remove that , use 

    Comments
    • Looking at your data, do you have blanks in some columns? try str(t1[1,]) and see if it's doing what you expect.
    • Could you paste in the first few lines of the file?
    • Does this answer your question? Row to colnames
    • yes you are missing two steps, first you need to remove the first row which you are using as column names and convert the matrix to data.frame