我有一个与循环函数有关的问题,我已经定义了一个类似的函数:
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"
。我可以在运行unif.score
之前解决声明FUN
的问题,但我想了解此错误消息的行为,我认为它可能与环境有关,但我不确定,
非常感谢
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
答案 0 :(得分:2)
这与范围和ddply
查找函数和变量的位置有关。这就是添加函数here
的原因(我认为在2012年)。使用它应该有效:
scores.v2 <- ddply(scores.v2, "Locus", here(transform), Score = unif.score(Score))