2015-05-23 34 views
1

我希望有兩個相同的號碼,並將其插入到set.seed()和文件名,如下如何製作兩個相同的數字並插入set.seed()和文件名?

set.seed(8888) 
    write(rnorm(500),"theta_8888_g.txt", sep=",", ncol=500) 
    set.seed(9999) 
    write(rnorm(500),"theta_9999_g.txt", sep=",", ncol=500) 
    ,,,,,,, 
    set.seed(111111) 
    write(rnorm(500),"theta_1111_g.txt", sep=",", ncol=500) 

預先感謝您

回答

2
y <- 8888 
set.seed(y) 
write(rnorm(500), paste0("theta_", y, "_g.txt"), sep=",", ncol=500) 

而對於更廣義的解決方案,創建一個功能:

write.theta <- function(x) { 
    set.seed(x) 
    write(rnorm(500), paste0("theta_", x, "_g.txt"), sep=",", ncol=500) 
} 

然後,您可以

write.theta(8888) 

or

y <- c(8888, 9999, 1111) 
sapply(y, write.theta) 
相關問題