user3793937
user3793937

Reputation:

Save results of each for loop as new data frame

This is my first post ever so apologies up front for any inconvienance.

I'm trying to extract RNAseq data from cBioPortal with their function getProfileData(). I want to call this function on each of elements of my list with parameters generated from element of this list. I included a library, example cancers and example genes that can be called by this function.

library(cgdsr)
mycgds = CGDS("http://www.cbioportal.org/")

cancers1 = c("cesc_tcga", "ov_tcga", "ucs_tcga", "ucec_tcga")
genes = c("PTCH1", "PTCH2")

mRNAseqExtractor <- function(){   
  for(i in cancers1){
    i_RNAseq <- paste(i, "_rna_seq_v2_mrna", sep="")        
    i_all <- paste(i, "_all", sep="")        
    getProfileData(mycgds, genes, i_RNAseq, i_all)   } }

mRNAseqExtractor()

Basically, i want each iteration of this loop to save output of this getProfileData(mycgds, hedgehog_genes, i_RNAseq, i_all) to a new data frame.

PS. I was looking for similar post but couldn't find any that generate new global data frames in each iteration.

Upvotes: 1

Views: 923

Answers (1)

Simon Larsen
Simon Larsen

Reputation: 742

You can use lapply to return a list of dataframes.

profiles <- lapply(cancers1, function(i) {
    i_RNAseq <- paste(i, "_rna_seq_v2_mrna", sep="")        
    i_all <- paste(i, "_all", sep="")        
    getProfileData(mycgds, genes, i_RNAseq, i_all)
})

You can then access the individual data frames like this:

# access first data frame
print(profiles[[1]])

Upvotes: 3

Related Questions