在R中从循环中返回单独的txt文件 [英] Returning separate txt files from loop in R
问题描述
我想在循环中返回每个迭代的结果,写在一个单独的文本文件中,但由于某种原因,似乎并不奏效。我的代码是:
$ p $ for(i in length(traject)){
player< -subset(traject [ [i]],subset =(dt == 1),)
test< -player
write.table(test,file = paste(i,test.txt,sep =) )
头(测试)
}
只返回最后一次迭代,我做错了什么产生的单独的文本文件中的所有迭代的结果?
额外信息:循环是针对每个单独的玩家(具有不同的ID)通过adehabitatLT包中的as.ltraj()获取数据。
$ b
(我知道这个论坛有类似的问题,但没有人可以帮助我解决这个问题) >解决方案 也许你应该在中添加
1:length(traject)
循环如下:
for(i in 1:length(traject)){do something}
$你的循环只返回一个iteretion(最后一个),因为你的索引只是 length(traject)
> / code>,而应该使用 1:length(traject)
作为索引 i
从第一个元素到最后一个元素,你也可以用 seq_len(length(traject))来替换
1:length(traject)
c $ c>
I want to return the results of each iteration in a loop to be written in a separate text file, but for some reason it does not seem to work. My code is:
for (i in length(traject)){
player <-subset(traject[[i]],subset=(dt==1),)
test<-player
write.table(test, file=paste(i, "test.txt", sep=" "))
head(test)
}
Which only return the last iteration, what do i do wrong to produce the results of all iterations in separate text files?
Extra info: The loop is for each separate player (with different ID's) to gain data via as.ltraj() from the adehabitatLT package.
(i know there are similar questions on this forum but none could help me in solving this problem)
Maybe you should add 1:length(traject)
in your for
loop as in:
for (i in 1:length(traject)){ do something }
Your loop is only returning one iteretion (the last one) because your index is just length(traject)
instead you should use 1:length(traject)
for the index i
to move from the first element all the way to the last one, you can also replace 1:length(traject)
by seq_len(length(traject))
这篇关于在R中从循环中返回单独的txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!