Question

I have this text dataframe with all columns being character vectors.

    Gene.ID                     barcodes              value
    A2M          TCGA-BA-5149-01A-01D-1512-08        Missense_Mutation
   ABCC10        TCGA-BA-5559-01A-01D-1512-08        Missense_Mutation
   ABCC11        TCGA-BA-5557-01A-01D-1512-08        Silent
   ABCC8         TCGA-BA-5555-01A-01D-1512-08        Missense_Mutation
   ABHD5         TCGA-BA-5149-01A-01D-1512-08        Missense_Mutation
   ACCN1         TCGA-BA-5149-01A-01D-1512-08        Missense_Mutation

How do I build a dataframe from this using reshape/reshape 2 such that I get a dataframe of the format Gene.ID~barcodes and the values being the text in the value column for each and "NA" or "WT" for a filler?

The default aggregation function keeps defaulting to length, which I want to avoid if possible.

Was it helpful?

Solution

I think this will work for your problem. First, I'm generating some data similar to yours. I'm making gene.id and barcode a factor for simplicity and this should be the same as your data.

geneNames <- c(paste("gene", 1:10, sep = ""))
data <- data.frame(gene = as.factor(c(1:10, 1:4, 6:10)),
                   express = sample(c("Silent", "Missense_Mutation"), 19, TRUE),
                   barcode = as.factor(c(rep(1, 10), rep(2, 9))))

I made a vector geneNames a vector of the gene names (e.g, A2M). In order to get the NA values in those missing an expression of a given gene, you need to merge the data such that you have number_of_genes by number_of_barcodes rows.

geneID <- unique(data$gene)
data2 <- data.frame(barcode = rep(unique(data$barcode), each = length(geneID)),
                    gene = geneID)
data3 <- merge(data, data2, by = c("barcode", "gene"), all.y = TRUE)

Now melting and casting the data,

library(reshape)
mdata3 <- melt(data3, id.vars = c("barcode", "gene"))
cdata <- cast(mdata3, barcode ~ variable + gene, identity)
names(cdata) <- c("barcode", geneNames)

You should then have a data frame with number_of_barcodes rows and with (number_of_unique_genes + 1) columns. Each column should contain the expression information for that particular gene in that particular sample barcode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top