1

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

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)}

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"

However, I can't find the files anywhere in my computer and I'm wondering if formulated the write.csv function correctly..

Any tips? Any help is appreciated!

2
  • 1
    In your write.csv, 'x' argument is not present. The file argument would be collaraccuracy1'. Perhaps x is collar? i.e. write.csv(x = collar, collaraccuracy1)} Commented Feb 12, 2019 at 11:52
  • 1
    Great! Thank you! Commented Feb 12, 2019 at 11:59

1 Answer 1

1

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)
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.