0

Consider the following data frames

df1 df2

df1 <- data.frame(ID = 1:10,
                  Score = c(73, 53, 8, 100, 39, 62, 78, 89, 61, 18))

df2 <- data.frame(Category = c(10, 20, 30, 40, 50, 60, 70, 80, 90),
                  Score_min = c(1, 21, 31, 41, 51, 61, 71, 81, 91),
                  Score_max = c(20, 30, 40, 50, 60, 70, 80, 90, 100))

I want to see which df2$Category each df1$Score is by testing the range between columns df2$Score_min and df2$Score_max

Meaning, for each Score in df1, I want to see where it lies in the range between Score_min and Score_max in df2, and return the Category variable in df2

The output i am hoping to obtain is

final.df

final.df <- data.frame(ID = 1:10,
                       Score = c(73, 53, 8, 100, 39, 62, 78, 89, 61, 18)) %>% 
  mutate(Category = c(70, 50, 10, 90, 30, 60, 70, 80, 60, 20))

I have tried left_join and lapply with no luck.

1 Answer 1

0
library(dplyr)
df1 |>
  left_join(df2, join_by(Score >= Score_min, Score <= Score_max))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.