samedi 3 février 2018

if-else condition for multiple datafiles repeats output

i'd appreciate your help on something that is currently driving me crazy. I'm processing some data regarding different characteristics with a loop & everything besides this particular function is working properly:

fun<-function(X)
   {
     Y <- numeric()
     if(max(X)== X[1]){Y<-0}else{Y=X[which.max(X)-1]}
   return(Y)
  }

so: say, I got 5 different csv.-files in my directory, which i'm refering to. in a certain row there are values, which im comparing with the expression X[which.max(X)-1]

So for instance I have got 3 values in my file:

3.5;6.4;7.1  -> the function should give me 6.4
2.8;1.6;2.3  -> the function should give me 0

... and so on.

The function itself is working, but for 5 different files I always get the same output-value. In this case I would get five time 6.4 . So it just repeats the very first output and does not actually consider the values of the second file.

I hope i explained my problem accurate and would appreciate hints and suggestions :)

cheers, Olli

Aucun commentaire:

Enregistrer un commentaire