I have a dataframe with many column names. I want to convert these column names to a new data dataframe.
names(df)
'SampleID' 'EMTSCORE' 'ESR1' 'PGR' 'ADCY1' 'TFF1' 'TUBA3D' 'SUSD3' 'CLIC6' 'FGD3' 'SCUBE2' 'PDZK1' 'SERPINA3' 'Mean' 'GrpMedian' 'Quartile'
I want to convert them as a new dataframe with SampleID
as the header and other as row values?
SampleID
EMTSCORE
ESR1
PGR
...
...
Quartile
CodePudding user response:
A mock data frame to show how it works
df1 <- data.frame(SampleID = letters[1:5],
EMTSCORE = LETTERS[1:5],
ESR1 = 1:5)
create a data frame from the names except the first one
(that is what the index -1
is for).
df2 <- data.frame(Dummy = names(df1)[-1])
Use the name of the first column of the old data frame for the new one.
names(df2)[1] <- names(df1)[1]
df2
#> SampleID
#> 1 EMTSCORE
#> 2 ESR1
Created on 2021-09-30 by the reprex package (v2.0.1)
CodePudding user response:
Here is an example with the mtcars
dataset:
We could use names()
function:
df2 <- data.frame(Col1 = names(mtcars))
Col1
1 mpg
2 cyl
3 disp
4 hp
5 drat
6 wt
7 qsec
8 vs
9 am
10 gear
11 carb