making the first row a header in a dataframe in r

11,351

Solution 1

Works for me (with a little trick).

x <- read.table(text = "File Fp1.PD_ShortSOA_FAM Fp1.PD_LongSOA_FAM Fp1.PD_ShortSOA_SEMplus_REAL Fp1.PD_ShortSOA_SEMplus_FICT
    sub0001            0,446222          2,524,804            0,272959                    1,281,349
    sub0002           1,032,688          2,671,048           1,033,278                    1,217,817",
                header = TRUE)

x <- t(x)
colnames(x) <- x[1, ]
x <- x[-1, ]
x

                             sub0001     sub0002    
Fp1.PD_ShortSOA_FAM          "0,446222"  "1,032,688"
Fp1.PD_LongSOA_FAM           "2,524,804" "2,671,048"
Fp1.PD_ShortSOA_SEMplus_REAL "0,272959"  "1,033,278"
Fp1.PD_ShortSOA_SEMplus_FICT "1,281,349" "1,217,817"

Solution 2

We can make use of transpose from data.table

library(janitor)
data.table::transpose(x, keep.names = 'File') %>%
           row_to_names(1)
#                          File   sub0001   sub0002
#2          Fp1.PD_ShortSOA_FAM  0,446222 1,032,688
#3           Fp1.PD_LongSOA_FAM 2,524,804 2,671,048
#4 Fp1.PD_ShortSOA_SEMplus_REAL  0,272959 1,033,278
#5 Fp1.PD_ShortSOA_SEMplus_FICT 1,281,349 1,217,817

data

x <- structure(list(File = structure(1:2, .Label = c("sub0001", "sub0002"
), class = "factor"), Fp1.PD_ShortSOA_FAM = structure(1:2, .Label = c("0,446222", 
"1,032,688"), class = "factor"), Fp1.PD_LongSOA_FAM = structure(1:2, .Label = c("2,524,804", 
"2,671,048"), class = "factor"), Fp1.PD_ShortSOA_SEMplus_REAL = structure(1:2, .Label = c("0,272959", 
"1,033,278"), class = "factor"), Fp1.PD_ShortSOA_SEMplus_FICT = structure(2:1, .Label = c("1,217,817", 
"1,281,349"), class = "factor")), class = "data.frame", row.names = c(NA, 
-2L))
Share:
11,351
steve zissou
Author by

steve zissou

Updated on June 04, 2022

Comments

  • steve zissou
    steve zissou almost 2 years

    I've seen this asked here: Create header of a dataframe from the first row in the data frame

    and here: assign headers based on existing row in dataframe in R

    and the solutions offered don't work for me.

    When I transpose my dataframe (p1), the header of DF.transpose (p1t) is something new and annoying. and the first row of the p1t is what I would like to use as the header, I tried:

        colnames(p1t) = p1t[1, ] 
    

    and it doesn't work!

    here is how the original df appears:

        File Fp1.PD_ShortSOA_FAM Fp1.PD_LongSOA_FAM Fp1.PD_ShortSOA_SEMplus_REAL Fp1.PD_ShortSOA_SEMplus_FICT
        sub0001            0,446222          2,524,804            0,272959                    1,281,349
        sub0002           1,032,688          2,671,048           1,033,278                    1,217,817
    

    And here is how the transpose appears:

        row.names                            V1         V2
        File                            sub0001    sub0002
        Fp1.PD_ShortSOA_FAM            0,446222  1,032,688
        Fp1.PD_LongSOA_FAM            2,524,804  2,671,048
        Fp1.PD_ShortSOA_SEMplus_REAL   0,272959  1,033,278
        Fp1.PD_ShortSOA_SEMplus_FICT  1,281,349  1,217,817
        Fp1.PD_ShortSOA_SEMminus_REAL  0,142739  1,405,100
        Fp1.PD_ShortSOA_SEMminus_FICT 1,515,577 -1,990,458
    

    How can I make "File", "sub0001","sub0002" etc... as the header?

    Thanks!