user4381526
user4381526

Reputation:

Put the first row as the column names of my dataframe with dplyr in R

This is my dataframe:

x<-data.frame(A = c(letters[1:10]), M1 = c(11:20), M2 = c(31:40), M3 = c(41:50))

colnames(x)<-NULL

I want to tranpose (t(x)) and consider the first column of x as the colnames of the new dataframe t(x).

Also I need them (the colnames of t(x)) to be identified as words/letters (as character right?)

Is it possible to do this with dplyr package?

Any help?

Upvotes: 16

Views: 27584

Answers (5)

Zo&#235; Turner
Zo&#235; Turner

Reputation: 499

The {janitor} package is good for this and is flexible enough to be able to select any row to push to column names:


library(tidyverse)
library(janitor)

x <- x %>% row_to_names(row_number = 1)

Upvotes: 15

LC-datascientist
LC-datascientist

Reputation: 2096

I think column_to_rownames from the tibble package would be your simplest solution. Use it before you transpose with t.

library(magrittr)
library(tibble)

x %>% 
  column_to_rownames("A") %>% 
  t

#>     a  b  c  d  e  f  g  h  i  j
#> M1 11 12 13 14 15 16 17 18 19 20
#> M2 31 32 33 34 35 36 37 38 39 40
#> M3 41 42 43 44 45 46 47 48 49 50

The "M1", "M2", "M3" above are row names. If you want to keep them inside (as a column), you can add rownames_to_column from the same package.

x %>% 
  column_to_rownames("A") %>% 
  t %>% 
  as.data.frame %>% 
  rownames_to_column("key")

#>   key  a  b  c  d  e  f  g  h  i  j
#> 1  M1 11 12 13 14 15 16 17 18 19 20
#> 2  M2 31 32 33 34 35 36 37 38 39 40
#> 3  M3 41 42 43 44 45 46 47 48 49 50

Essentially,
column_to_rownames("A") converts column "A" in x to row names,
t transposes the data.frame (now a matrix),
as.data.frame reclassifies it back as a data.frame (which is necessary for the next function), and
rownames_to_column("key") converts the row names into a new column called "key".

Upvotes: 5

Tung
Tung

Reputation: 28401

Using rownames_to_column() from the tibble package

library(magrittr)
library(tibble)

x %>%
  t() %>%
  as.data.frame(stringsAsFactors = FALSE) %>%
  rownames_to_column() %>%
  `colnames<-`(.[1,]) %>%
  .[-1,] %>%
  `rownames<-`(NULL)
#>    A  a  b  c  d  e  f  g  h  i  j
#> 1 M1 11 12 13 14 15 16 17 18 19 20
#> 2 M2 31 32 33 34 35 36 37 38 39 40
#> 3 M3 41 42 43 44 45 46 47 48 49 50

x %>% 
  `row.names<-`(.[, 1]) %>% 
  t() %>%
  as.data.frame(stringsAsFactors = FALSE) %>% 
  .[-1,] 
#>     a  b  c  d  e  f  g  h  i  j
#> M1 11 12 13 14 15 16 17 18 19 20
#> M2 31 32 33 34 35 36 37 38 39 40
#> M3 41 42 43 44 45 46 47 48 49 50

Created on 2018-10-06 by the reprex package (v0.2.1.9000)

Upvotes: 2

Samuel
Samuel

Reputation: 3053

Try this:

library(dplyr)
library(tidyr)

x <- data.frame(
  A = c(letters[1:10]),
  M1 = c(11:20),
  M2 = c(31:40),
  M3 = c(41:50))

x %>% 
  gather(key = key, value = value, 2:ncol(x)) %>% 
  spread(key = names(x)[1], value = "value")
  key  a  b  c  d  e  f  g  h  i  j
1  M1 11 12 13 14 15 16 17 18 19 20
2  M2 31 32 33 34 35 36 37 38 39 40
3  M3 41 42 43 44 45 46 47 48 49 50

Upvotes: 3

G5W
G5W

Reputation: 37661

You can do this easily in base R. Just make the first column of x be the row names, then remove the first column and transpose.

row.names(x) = x[,1]
x = t(x[,-1])
x
    a  b  c  d  e  f  g  h  i  j
M1 11 12 13 14 15 16 17 18 19 20
M2 31 32 33 34 35 36 37 38 39 40
M3 41 42 43 44 45 46 47 48 49 50

Upvotes: 4

Related Questions