0

I'm searching for the maximum value within a certain range of rows in a CSV file. I figured out the range, but now that I want to determine the maximum value it just reveals the position of that particular value instead of the actual value.

Initial situation as follows:

First I read the file

A <- read.csv(file=data[1], header=TRUE, sep=";")

Then I determine the range investigating

Duration = as.character (A [16,1])
Duration = as.numeric(strsplit(Duration," ") [[1]][2])

B<- A[which(A==28447)]

so these are the target rows:

[943] 0.0                                                                                     
[944] 1.8771                                                                                  
[945] 1.2928                                                                                  
[946] 1.7946                                                                                  
[947] 2.3201                                                                                  
[948] 1.8459                                                                                  
[949] 1.5889                                                                                  
[950] 1.3549                                                                                  
[951] 1.2376                                                                                  
[952] 1.1296                                                                                  
[953] 1.0619                                                                                  
[954] 1.0132                                                                                  
[955] 0.9684                                                                                  
[956] 0.93                                                                                    
[957] 0.8976

now what I did was:

A[(B+1):(B+Duration),1]

revealing that:

    [1] 0.0    1.8771 1.2928 1.7946 2.3201 1.8459 1.5889 1.3549 1.2376 1.1296 1.0619 1.0132 0.9684 0.93   0.8976
24582 Levels: -0.0 -0.001 -0.0011 -0.0012 -0.0014 -0.0016 -0.0017 -0.0018 -0.0019 -0.0022 -0.0024 -0.0026 -0.0028 -0.003 -0.0033 ... Timesteps: 15

If I now go for the max((B+1):(B+Duration),1) I get:

957

How do I avoid that?

10 Rep
  • 2,217
  • 7
  • 19
  • 33
Olli
  • 295
  • 4
  • 14

2 Answers2

3

which.max appropriately1 returns the index. You just want max instead.


1 which in R function names usually refers to indices. For example, which(A[, 1] > 2) will return the indices of all elements greater than 2.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
1

which.max returns the position of the maximum value in the vector:

> A <-  rnorm(10,0,1)
> A
 [1] -1.05831869 -0.20686273 -0.81552863 -0.47213498  0.87199615  0.06288441  0.94777881  0.46776767  1.91153029
[10] -0.59206801
> 
> which.max(A)
[1] 9

So, to illustrate this, you could use this to index to the maximum value:

> A[which.max(A)]
[1] 1.91153

Much simpler though is the max function!

> max(A)
[1] 1.91153
> 

If you're getting a not meaningful for factors error you need to convert to numeric first. How to convert a factor to integer\numeric without loss of information?

rg255
  • 4,119
  • 3
  • 22
  • 40