lundi 28 juin 2021

R - excel match/index alternative

I have this dataframe

t1 <-
    data.frame(
      amt = c(13500, 12500, 11500, 10500, 9000, 0),
      "0" = seq(0.8, 1.3, by = 0.1),
      "50" = seq(0.7, 1.2, by = 0.1),
      "56" = seq(0.6, 1.1, by = 0.1),
      "61" = seq(0.5, 1, by = 0.1)
    )

And this second dataframe

t2 <-
    data.frame(
      id = seq(1, 5),
      percentage = c(0, 50, 55, 80, 61),
      created_amount = c(12000, 9000, 50, 40000, 10000)
    )

My goal is to create third table, that will compare created amount from t2 dataframe with first column in t1 dataframe and return first higher or equal row and also compares percentage with 0, 50, 56 and 61 columns and if percentage is equal or higher, then it will select this colum.

Result should be

result <-
    data.frame(
      id = seq(1, 5),
      percentage = c(0, 50, 55, 80, 61),
      created_amount = c(12000, 9000, 50, 40000, 10000)
      result = c(1.0, 1.1, 1.0, 0.5, 0.5)
    )

In excel I would you match and index function, in R it could be solved via dome if else cution, but if else solution doesnt seem to be robust enough.

I found some tutorials for that but I need one that works with dataframes.

Aucun commentaire:

Enregistrer un commentaire