With base R
df_new <- data.frame(t(df[,-c(1:2)]))
colnames(df_new) <- df$Name
Or with tidyverse
:
library(tidyverse)
df_new <- df %>%
select(-Description) %>%
rowid_to_column() %>%
pivot_longer(-c(Name, rowid)) %>%
pivot_wider(names_from = c("Name", "rowid"), values_from = "value")
colnames(df_new) <- str_replace_all(colnames(df_new), "\\_[0-9]","")
Output
ENSG0~ ENSG0~ ENSG0~
GTEX-1117F-022~ 0 187 0
GTEX-1117F-042~ 0 109 0
Another option if you need to keep the Description
would be to combine the Name
and Description
in the column heading.
library(tidyverse)
df %>%
pivot_longer(-c(Name, Description)) %>%
pivot_wider(names_from = c("Name", "Description"), values_from = "value")
name `ENSG0~_DDX11L1` `ENSG0~_WASH7p` `ENSG0~_MIR6859-1`
<chr> <dbl> <dbl> <dbl>
1 GTEX-1117F-022~ 0 187 0
2 GTEX-1117F-042~ 0 109 0
Data
df <- structure(list(Name = c("ENSG0~", "ENSG0~", "ENSG0~"), Description = c("DDX11L1",
"WASH7p", "MIR6859-1"), `GTEX-1117F-022~` = c(0, 187, 0), `GTEX-1117F-042~` = c(0,
109, 0)), class = "data.frame", row.names = c(NA, -3L))