Conditional statement combining case_when and mutate

允我心安 提交于 2020-12-13 04:23:06

问题


I want create a bivariate map plotting two variables: production and possession. In order to give part of the data the correct colour I want to add a column with color codes "A", "B", "C" for one variable and for the other 1, 2, 3. Then later concatinating the two. Just so that the data is coded like the following example:

Here's my example df and failing code:

library(dplyr)

example_df <- structure(list(production = c(0.74, 1.34, 2.5), possession = c(5, 
23.8, 124.89)), .Names = c("production", "possession"), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))

example_df %>%
  mutate(colour_class_nr = case_when(.$production %in% 0.068:0.608 ~ "1",
                                     .$production %in% 0.609:1.502 ~ "2",
                                     .$production %in% 1.503:3.061 ~ "3",
                                     TRUE ~ "none"),
         colour_class_letter = case_when(.$possession %in% 0.276:9.6 ~ "A",
                                         .$possession %in% 9.7:52 ~ "B",
                                         .$possession %in% 52.1:155.3 ~ "C",
                                         TRUE ~ "none"))

With these results...:

# A tibble: 3 x 4
  production possession colour_class_nr colour_class_letter
       <dbl>      <dbl> <chr>           <chr>              
1      0.740       5.00 4               none               
2      1.34       23.8  4               none               
3      2.50      125    4               none  

But this IS the desired output:

# A tibble: 3 x 4
  production possession colour_class_nr colour_class_letter
       <dbl>      <dbl>           <dbl> <chr>              
1      0.740       5.00 2                A               
2      1.34       23.8  2                B               
3      2.50      125    3                C 

I'm new with case_when() incombination with mutate, hope someone can help.


回答1:


Maybe this:

example_df %>%
  mutate(colour_class_nr = case_when(production < 0.608 ~ "1",
                                     production > 0.609 & production < 1.502 ~ "2",
                                     production > 1.503 ~ "3",
                                     TRUE ~ "none"),
         colour_class_letter = case_when(possession < 9.6 ~ "A",
                                         possession > 9.6 & possession < 52 ~ "B",
                                         possession > 52 ~ "C",
                                         TRUE ~ "none"))

Result:

# A tibble: 3 x 4
  production possession colour_class_nr colour_class_letter
       <dbl>      <dbl> <chr>           <chr>              
1      0.740       5.00 2               A                  
2      1.34       23.8  2               B                  
3      2.50      125    3               C

The only difference is the use of >and <, although some of the conditions don't make a whole lot of sense in your example. You also don't need the .$ in the latest versions of dplyr.



来源:https://stackoverflow.com/questions/48988495/conditional-statement-combining-case-when-and-mutate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!