为循环输出创建多个文件 [英] Creating multiple files for for-loop outputs
问题描述
我是R语言的初学者,所以我希望这个问题对大多数人都有意义。
I'm quite a beginner in R so I hope this question is interesting to most of you.
这里是项圈的示例
我正在使用的文件:
Here's a sample of the collar
file I'm working with:
observed predicted probability results1
1 Head-up Grazing 0.2727273 NEGATIVE
2 Head-up Grazing 0.7272727 NEGATIVE
3 Head-up Grazing 0.7272727 NEGATIVE
4 Head-up Grazing 0.5454545 NEGATIVE
5 Head-up Grazing 0.7272727 NEGATIVE
6 Head-up Grazing 0.4545455 NEGATIVE
7 Head-up Vigilance 0.3636364 NEGATIVE
8 Head-up Grazing 0.3636364 NEGATIVE
9 Head-up Vigilance 0.3636364 NEGATIVE
10 Unknown Grazing 0.3636364 NEGATIVE
11 Moving Head-up 0.4545455 NEGATIVE
12 Moving Grazing 0.3636364 NEGATIVE
13 Head-up Grazing 0.4545455 NEGATIVE
14 Head-up Grazing 0.3636364 NEGATIVE
15 Head-up Grazing 0.4545455 NEGATIVE
16 Head-up Grazing 0.3636364 NEGATIVE
17 Head-up Head-up 0.4545455 POSITIVE
18 Head-up Grazing 0.2727273 NEGATIVE
接下来,我打算创建一个 for
循环,该循环将添加第5列 results2
。因为第五列的结果 results2
取决于 i
的值,范围从0到1,并且增加0.1 ,我想为每个 i
值(其中 i = 0
, i = 0.1
等,直到 i = 1
)。到目前为止,我一直在尝试以下操作:
Next, I intended to create a for
loop that will add a 5th column "results2"
. Because the outcome of the 5th column "results2"
depends on a i
value ranging from 0 to 1 and increasing of 0.1, I want to create multiple Excel files for each i
value (where i=0
, i=0.1
etc. until i=1
). Here's what I've been trying so far:
#Creating the for loop for column results 2 with i [0:1] increasing of 0.1. The file collar is the full file from the sample above.
for (i in seq(0, 1, by = 0.1))
{collar$results2<-mutate(collar,results2 = case_when( (probability > i & results1 == "POSITIVE") | (probability < i & results1 == "NEGATIVE") ~ TRUE, TRUE ~ FALSE) )
as.character(collar$results2)
#Writing down Excel files for each i value
collaraccuracy1=paste('collar41361_41365', i, 'csv', sep = '.')
write.csv(collaraccuracy1)}
这是运行循环时R打印的内容。名称与我希望的名称完全相同:
This is what is printed by R when running the loop. Names are exactly as I want them to be:
"","x"
"1","collar41361_41365.0.csv"
"","x"
"1","collar41361_41365.0.1.csv"
"","x"
"1","collar41361_41365.0.2.csv"
"","x"
"1","collar41361_41365.0.3.csv"
"","x"
"1","collar41361_41365.0.4.csv"
"","x"
"1","collar41361_41365.0.5.csv"
"","x"
"1","collar41361_41365.0.6.csv"
"","x"
"1","collar41361_41365.0.7.csv"
"","x"
"1","collar41361_41365.0.8.csv"
"","x"
"1","collar41361_41365.0.9.csv"
"","x"
"1","collar41361_41365.1.csv"
但是,我在计算机的任何位置都找不到文件,我想知道是否正确编写了 write.csv
函数。
However, I can't find the files anywhere in my computer and I'm wondering if formulated the write.csv
function correctly..
有什么提示吗?
推荐答案
问题是 write.csv
需要循环中未提供的对象参数 x。它将'collaraccuracy1'感知为文件
。
The issue is that write.csv
needs an object parameter 'x' which is not provided in the loop. It senses 'collaraccuracy1' as the file
.
...
write.csv(x = collar, collaraccuracy1)
}
这篇关于为循环输出创建多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!