0

I wrote a code to compute the amount of second data set when the amount of the first data set is maximum but at the end for some latitude and longitude I got very large number which is not in the first data set. Here is the core of the code:

PW.storm <- array(NA,dim=c(length(lon1),length(lat1))) for (i in 1:length(lon1)){ for (j in 1:length(lat1)){ for (k in 1:length(time)){ t <- time.when.dat1.max <- which.max(abs(dat1[i,j,])) PW.storm[i,j] <- abs(dat2[i,j,t]) } } } 
2
  • 7
    Is there a question here? (I heard they typically end with a question mark.) Commented Jul 22, 2013 at 17:29
  • Do note the tie-handling properties of which.max - read ?which.max and try which(x == max(x) as an alternative if you have ties. Commented Jul 22, 2013 at 18:05

1 Answer 1

1

You code suggests you do not understand what is being returned by which.max. You have:

time.when.dat1.max <- which.max(abs(dat1[i,j,])) 

That is not the "time when dat1 is max". It is the index in dat1's third dimension under the conditions of i and j. If times were the values in that third dimension then you would need to use this to get the time values:

 time.when.dat1.max <- dat1[i,j, which.max(abs(dat1[i,j,]))] 

If I've guessed wrong about what the dat1 object holds, then you should do a better job of describing the data setup by editing your question.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.