I want to do this: Input:
For the output i want to have the GTEX id as rows and ENSG as columns. Like this:
SAMPLEID ENSGO ENSGO ENSGO
GTEX-1117F-022~ 0 187 0
GTEX-1117F-042~ 0 109 0
I tried to do this command but it doesn't give me the correct output:
GTEx_Analysis <- as.data.frame(t(GTEx_Analysis_2017_06_05_v8_RNASeQCv1_1_9_gene_reads))
CodePudding user response:
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))