assign headers based on existing row in dataframe in R

后端 未结 5 605
谎友^
谎友^ 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: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.

提交回复
热议问题