Logical condition on a dataframe

Asked

Viewed 46 times

1

After finding a value of a calculation I need to compare if this value is equal to any of the first column of the table below (percentile), if it is equal, the corresponding value will be selected but that of the second column (values). And if it’s not the same, I have to interpolate between the two values closest to which it lies.

For example: I found the value equal to 0.1 and I will compare it with the first column of the table and in this case, the value corresponding to it in the second column would be 0.0158. But if the value was 0.93, I would have to interpolate between 2,706 and 3,841 which are the values corresponding to 0.90 and 0.95.

The table is as follows:

tabelaq <- data.frame(percentil=c(0.005,0.01,0.025,0.05,0.1,0.25,0.75,0.90,0.95,0.975,0.99,0.995), 
  valores=c(0.0000393,0.000157,0.000982,0.00393,0.0158, 0.102,1.323,2.706,3.841,5.024,6.635,7.879))

I tried logical conditions with for and if, but it’s giving error. I tried subset(tabelaq,percentil==t, select = valores) inside if and did not give.

I’m trying on the R.

Can anyone suggest me an idea?

1 answer

2


The function below solves the problem. It is commented, but basically it tests whether x is in percentil. If it is, you only find the corresponding value. If x is not in percentil, she finds in which interval x is and interpolates the desired values.

In the absence of further instructions, I ended up making a linear interpolation between the values, that is, I took the arithmetic mean between them.

tabelaq <- data.frame(percentil=c(0.005,0.01,0.025,0.05,0.1,0.25,0.75,0.90,
                                0.95,0.975,0.99,0.995), 
                      valores=c(0.0000393,0.000157,0.000982,0.00393,0.0158, 
                                0.102,1.323,2.706,3.841,5.024,6.635,7.879))


funcao <- function(x, tabela){

  # procura se x estah nos percentis
  
  if (x %in% tabela[, 1]) {
    
    # em caso positivo, retorna o valor correspondente
    valor <- tabela[tabela[, 1] == x, 2]
  } else {
    
    # em caso negativo, encontra o intervalo em que x estah
    # e faz a interpolacao em seguida
    indice <- findInterval(x, tabelaq$percentil)
    valor  <- (tabela[indice, 2] + tabela[indice+1, 2])/2
  }
  
  # testa se o resultado esta dentro de algum intervalo fora das caudas
  # se nao estiver, retorna NA
  if (length(valor) == 0) {
    return(NA)
  } else {
    return(valor)
  }
  
}

funcao(0.1, tabelaq)
#> [1] 0.0158
funcao(0.93, tabelaq)
#> [1] 3.2735
funcao(0.000001, tabelaq)
#> [1] NA
funcao(0.999999, tabelaq)
#> [1] NA

Created on 2021-06-08 by the reprex package (v2.0.0)

  • God, you saved me so much. God bless you! Regarding the interpolation, I thought about the approx function, it performs the linear interpolation, hence it would be approx(table[,1]l,table[,2], xout=0.9875). xout is the value of x to be interpolated which in this case would be x (xout=x). Gratitude!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.