加快创建对象和应用函数

时间:2018-06-01 22:22:04

标签: r loops ggplot2 apply

我正在努力加快以下任务:

# For retrieving Cancer Genome Atlas RNAseq data

library(RTCGA.rnaseq)



# Extract information from sample ID about what kind of samples are in the dataset. 01 means tumor, 06 means metastatic tumor, 10 means healthy etc.

SKCM_sampletype <- as.factor(substr(x = SKCM.rnaseq$bcr_patient_barcode, 14, 15))
summary(SKCM_sampletype)

  01  06 
   1 367 

# Other objects I like to apply this function are (I have 30some objects but showing few below:
# (ACC.rnaseq,BLCA.rnaseq,BRCA.rnaseq,CESC.rnaseq,CHOL.rnaseq,COAD.rnaseq)

我想要做的是拥有一段代码,它将遍历一个对象列表并执行substringsummary函数。我还想将所有summary统计数据和图表组合在一个如下所示的图表中:

sample image

我的主要问题与两个要点有关: 1-如何编写将使用对象的loop(或apply?)函数 2-组织数据以便与ggplot2包一起使用的最佳方法是什么。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以尝试在感兴趣的列中使用lapply,例如:

summarise_column <- function(x) {
  cat(x)
  summary(factor(substr(SKCM.rnaseq[[x]], 14, 15)))
}

lapply(c("the", "variables", "I", "would", "like", "to", "summarise"), summarise_column)