2017-04-14 75 views
3

我是新来的R和我有RNA测序结果的25个样品。我想应用相同的函数来计算我的目标基因(如基因ABC)与所有25个样本的相关性。应用到多个数据集相同功能中的R

我知道如何单独做到这一点。这里是我的代码做到这一点:

df <- read.table("Sample1.txt", header=T, sep="\t") 

# gene expression values of interest 
gene <-as.numeric(df["ABC",]) 

# correlate gene with all others genes in the expression set 
correlations <- apply(df,1,function(x){cor(gene,x)}) 

但现在我有他们25。我用乐器一次读完。

data <- c("Sample1.txt", "Sample2.txt",..."Sample25.txt") 
df <- lapply(data, read.table) 
names(df) <- data 

但是,我失去了如何将它与我上面的其他代码连接起来计算基因相关性。我已经阅读了一些相关的主题,但仍然无法弄清楚。任何人都可以帮我吗?谢谢!

+0

这将有助于了解你的输入和输出看起来像在这里,作为答案实际上取决于事情如何格式化。 – caw5cv

+3

总结你的第一块块的,需要一个文件名,并返回'correlations'功能。然后,做'lapply(数据,yourFunction中)' –

+0

@Marat,你能请提供一个答案?对不起,我对语言理解有点新。 – kin182

回答

2

你应该这样做:

files <- c("Sample1.txt", "Sample2.txt", ..., "Sample25.txt") 

myfunc <- function(file) { 
    df <- read.table(file, header=TRUE, sep="\t") 

    # gene expression values of interest 
    gene <- as.numeric(df["ABC",]) 

    # correlate gene with all others genes in the expression set 
    correlations <- apply(df, 1, function(x) cor(gene, x)) 
} 

lapply(files, myfunc) 

那就是我为您推荐的风格。这是我的风格会做:

myfunc <- function(file) { 
    df <- read.table(file, header=TRUE, sep="\t") 
    gene <- as.numeric(df["ABC",]) # gene expression values of interest 
    apply(df, 1, FUN=cor, y=gene) # correlate gene with all others 
} 

files <- c("Sample1.txt", "Sample2.txt", ..., "Sample25.txt") 
lapply(files, myfunc) 

也许你想要保存结果的对象:

L <- lapply(files, myfunc) 

对于功能甚至可以做(因为cor()采用矩阵参数)):

myfunc <- function(file) { 
    df <- read.table(file, header=TRUE, sep="\t") 
    cor(t(df), y=as.numeric(df["ABC",])) # correlate gene with all others 
} 
+0

我在match.fun(FUN)的错误'错误:参数“FUN”缺失,没有default'。但是,我不知道这意味着什么。 – kin182

+0

什么源代码行给出了该错误? – jogo

+0

@ jogo的答案正是我所要做的,只是不要忘记将生成的列表保存在一个变量中(例如'L < - lapply(files,myfunc)')。错误消息可能来自应用程序或lapply,但答案中的语法似乎是正确的。 –