我做了简单的循环并将结果打印出来但不确定如何输出。
这就是我编码的内容:
>for (i in 0:45) for (j in 0:45) print(i/j)
[1] Inf
[1] 1
[1] 0.5
[1] 0.3333333
[1] 0.25
[1] 0.2
[1] 0.1666667
[1] 0.1428571
[1] 0.125
[1] 0.1111111
[1] 0.1
从这里开始,我怎样才能保存这个结果?我应该将打印(i / j)打印到对象中还是有其他方法将其保存到文件中? 谢谢,
答案 0 :(得分:14)
有两个选项,具体取决于您想要的输出。
1)capture.output()
会将循环的输出抓取到文件中:
capture.output(for (i in 0:45) for (j in 0:45) print(i/j),
file = "foo.txt")
2)如果你想要数字,那么通过i/j
将save()
保存为R对象,或通过write.csv()
保存为文本文件(例如csv),不要打印它
out <- c() ## NEVER write a loop like this! Always allocate storage & fill in
for(i in 0:45)
for(j in 0:45)
out <- c(out, i/j)
head(out)
save(out, "foo.rda")
write.csv(out, "foo.csv")
但是,您需要了解R中的矢量化操作。在这种情况下,通过两个循环在R中进行的操作可以更有效地进行:
out2 <- outer(0:45, 0:45, "/")
答案 1 :(得分:4)
这实际上取决于你想做什么。如果您只想将输出捕获到文本文件中,则capture.output
,cat
或sink
中的一个是要查看的函数。如果要创建R对象以供稍后在会话中使用,则创建具有所需结构的对象:vector,list,matrix或data.frame。然后使用save
函数保存对象。可以使用dput
或dump
创建对象的文本表示。
答案 2 :(得分:1)
我碰巧打开了一个写入文件的函数。我使用了sink()(参见DWin和Gavin对其他解决方案的回答)
sink(file = file.name, type = "output")
cat("/* File created on", date(), "*/\n")
cat("/* Walker density:", walk.dens, "*/\n")
cat("/* Capture history has", nchar(as.character(cap.hist[1,])),
"sessions and", nrow(cap.hist), "walkers", "*/\n")
cat("/* number of initial walkers:", params$num.walker, "*/\n")
cat("/* number of steps per walker:", params$n.steps, "*/\n")
cat("/* area size:", params$area, "*/\n")
cat("/* home range:", params$home.range, "*/\n")
cat("/* number of bins:", params$num.bins, "*/\n")
cat("/* capture probability:", params$prob, "*/\n")
cat("/* number of sessions:", params$sessions, "*/\n")
cat("/* number of bootstraps:", params$num.boots, "*/\n")
cat("/* number of facies:", params$n.faces, "*/\n")
cat("/* working directory:", params$work.dir, "*/\n")
cat("/* number of cores for parallel:", params$num.cores, "*/\n")
cat("/* resolution of raster:", params$rsln, "*/\n")
cat("/* function used to modify resolution:", params$rsln.fun, "*/\n")
cat("/* created walk saved:", params$write.walk, "*/\n")
cat("/* columns: cap.hist/probs/world */\n\n")
apply(mat, 1, function(x) {
cat(x["cap.hist"], x["probs"], x["supop"], ";", "\n")
})
sink()
生成一个文件(这只是头部):
/* File created on Fri Feb 25 15:02:27 2011 */
/* Walker density: 0.001 */
/* Capture history has 40 entries and 67 number of walkers */
/* number of initial walkers: 200 */
/* number of steps per walker: 100 */
/* area size: 500 */
/* home range: 100 */
/* number of bins: 10 */
/* capture probability: 0.2 */
/* number of sessions: 40 */
/* number of lines per segment: */
/* number of bootstraps: 999 */
/* number of facies: 30 */
/* working directory: q:/walker/layers */
/* calculations done in parallel: */
/* number of cores for parallel: 4 */
/* resolution of raster: 5 */
/* function used to modify resolution: */
/* created walk saved: TRUE */
/* columns: cap.hist/probs/world */
1000000000010000100000000000000100000101 0.10876344 1 ;
1000010000000010011000000000001000010000 0.09428192 1 ;
0010000000001000001001101100000010000010 0.06079921 1 ;
0000101000000000000000000000000000001001 0.05272485 1 ;
1000000001101000001000000001000100000010 0.08599779 1 ;