2015-03-25 87 views
1

我有相关的函数中环的问题,我已经定义了一个函数那样:错误:找不到功能

FUN <- function(directory=dir, permutations=perm){ 

# Create directory 
dir.create(file.path(getwd(), directory), showWarnings=FALSE) 
mypath <- file.path(getwd(), directory) 

require('plyr') 
scores.v2 <- read.delim('scores.txt', stringsAsFactors=F) 

unif.score <- function(b) { 
    rand.score <- runif(length(b)) 
    new.score <- sort(rand.score/sum(rand.score), decreasing=T) 
    new.score 
} 

for (n in 1:permutations){ 

# Suffle gene_names in column genes 
scores.v2[,2] <- sample(scores.v2[,2]) 

# Assign new scores according to the uniform distribution 
scores.v2 <- ddply(scores.v2, "Locus", transform, Score=unif.score(Score)) 

# Order data.frame based on Locus and Score 
scores.v2 <- scores.v2[order(scores.v2[,1],-scores.v2[,5]),] 
return(scores.v2) 
    } 
} 

当我执行的功能FUN("permuted", 2)我得到的错误Error: could not find function "unif.score"。我可以在运行FUN之前修复声明unif.score的问题,但我想了解这个错误信息的行为,我认为这可能与环境,但我不知道,

非常感谢

的例子如何scores.txt外观:

Locus Gene Link.score Semantic.score  Score 
1 ABO  DBH 0.13924316  0.07621951 0.10773133 
2 ABO TSC1 0.00827303  0.18384146 0.09605725 
3 ABO SARDH 0.13924316  0.04573171 0.09248743 
4 ABO C9orf96 0.13924316  0.03506098 0.08715207 
5 ABO SLC2A6   NA  0.16859756 0.08429878 
6 ABO RALGDS 0.13924316  0.02195122 0.08059719 

回答

2

这是做范围和其中ddply查找函数和变量。这就是为什么添加功能here的原因(2012年,我认为)。使用这个应该工作:

scores.v2 <- ddply(scores.v2, "Locus", here(transform), Score = unif.score(Score))