Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

r - How can I create a new column based on conditional statements and dplyr?

x y
2 4
5 8
1 4
9 12

I have four conditions

  • maxx = 3, minx = 1, maxy = 6, miny = 3. (If minx < x < maxx and miny < y < maxy, then z = apple)
  • maxx = 6, minx = 4, maxy = 9, miny = 7. (If minx < x < maxx and miny < y < maxy, then z = ball)
  • maxx = 2, minx = 0, maxy = 5, miny = 3. (If minx < x < maxx and miny < y < maxy, then z = pine)
  • maxx = 12, minx = 7, maxy = 15, miny = 11. (If minx < x < maxx and miny < y < maxy, then z = orange)

Expected outcome:

x y z
2 4 apple
5 8 ball
1 4 pine 
9 12 orange

I have thousands of rows, and these four conditions that will fit all values.

How can I do this using the mutate function? I know how to manipulate numbers directly, but not sure how I can store a character based on conditional statements.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe the best option here is to use dplyr::case_when

df %>% mutate(z = case_when(
    x < 3  & x > 1 & y < 6  & y > 3  ~ "apple" ,
    x < 6  & x > 4 & y < 9  & y > 7  ~ "ball"  ,
    x < 2  & x > 0 & y < 5  & y > 3  ~ "pine"  ,
    x < 12 & x > 7 & y < 15 & y > 11 ~ "orange"
  )
)

Which gives us:

# A tibble: 4 x 3
      x     y z     
  <dbl> <dbl> <chr> 
1     2     4 apple 
2     5     8 ball  
3     1     4 pine  
4     9    12 orange

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...