如何在R中转置数据而不丢失信息?

时间:2017-11-15 12:32:13

标签: r

我希望转置我的数据集。

借口:当QIIME2输出时,' Feature.ID' (基本上是细菌种类)是行,站点是列,单元格值丰富。由于许多R软件包(如生物多样性)要求网站是行和种类作为列,我希望转换我的数据。

library(tidyverse)

Ftable <- read_tsv('table.feature-table_biom.txt', col_names = TRUE, skip = 1) #opens my QIIME2 file, removing the top line 

names(Ftable)[names(Ftable)=="#OTU ID"] <- "Feature.ID" #Renames the species column

Ftable <- cbind(Ftable, "observation"=1:nrow(Ftable)) #adds an indexing column 'observation' so that I can later remove the column containing the species names as they are complex and I do not wish to type them out

Ftable <- Ftable %>% select(observation, everything()) #Moves observation to the front

OtuOb <- Ftable

OtuOb <- as.tibble(OtuOb) 

write_tsv(OtuOb, "OtuToObservationReference.tsv") #These 3 lines save a reference so I can look to see which observations align to which species

Ftable <- Ftable[,-2] #removes 'Feature.ID' column so that the observation column shows the species

Ftable <- t(Ftable) #I have been trying to get this to work but it doesnt

Ftable <- as.tibble(Ftable) 

write_tsv(Ftable, "Ftable.tsv") 

当转置时,它会移除样本参考(沿着原始顶部),这意味着我无法看到它们如何与该物种的丰度相匹配。

Small sample of my data

1 个答案:

答案 0 :(得分:0)

在转置之前,将行名称分配给对象,进行转置,然后将存储的行名称分配给新的列名称:

($)