[R] Writing to Spreadsheet issues

Berend Hasselman
2013-03-11T09:08:04Z

On 10-03-2013, at 22:32, Louis <louisvar3 at gmail.com> wrote:

> I am having trouble with this code:
>
> trOne <- read.csv("*.csv", header=TRUE)
> srOne <- read.csv("*/SRdivision1.csv", header=TRUE)
> row = 1
>
> for (g in 1:162) {
> e = trOne[g, "END_TIME"]
> s = trOne[g, "START_TIME"]
> q = trOne[g, "OCC_TIME"]
> r = trOne[g, "R_TIME"]
> gazeSum = 0
> n = 0
> print(g)
>
> while (s <= e) {
>
> if (srOne[row, "timestamp"] == 670920) {
> print(srOne[row, "timestamp"]) }
>
> if (r == srOne[row, "timestamp"] ) {
> trOne[g, "R_EYE_POS"] = srOne[row, "LEFT_GAZE_X"] }
>
> if (q == srOne[row, "timestamp"] ) {
> trOne[g, "OCC_EYE_POS"] = srOne[row, "LEFT_GAZE_X"] }
>
> if (s >= q & s <= r) {
> gazeSum = gazeSum + as.double(srOne[row, "LEFT_GAZE_X"])
> n = n + 1 }
>
> s = s + 1
> row = row + 1
> }
> trOne[g, "AVG_EYE_POS"] = gazeSum / n
> }
>
> The code runs well, but there is an issue with this:
> Error in if (srOne[row, "timestamp"] == 670920) { :
>  missing value where TRUE/FALSE needed
>
> That is just a line for debugging, but it means the if statement below it
> isn't working right if I get the error. It only does this when I write
to
> file. The only unique thing in this case is that I know the line with
> 670920 as the timestamp has the Left_Gaze_X as 0 (as it should be) but the
> comparison being made is screwy after this row. The error shows up at this
> line, which is when g = 162. I have another file I used to get the rest of
> my data (SRDivision2), it calculates that incorrectly, Everything works
> fine before g = 162, though.
>
> After 162, when I make g 163:165, the Left_Gaze_X is correct, but when I
> set Occ_Eye_Pos equal to it, it equals a totally different number. For
> instance, the Gaze outputs as 488.3 when I print it, but when I set
> Occ_Eye_Pos equal to it, the value of Occ_Eye_Pos is 2838...
>
> Anyone know what's going on with this?

Not really. I am not clairvoyant and I don't have a magic wand to conjure up
your files.
read.csv accepts wildcards?

I would think that srOne[row, "timestamp"] at the moment of testing
does not contain what you think it does.
Most likely it is NA.
So you should determine the value of row when the error occurs and then print
srOne[row, "timestamp"] before the error happens.

Berend