结合case_when和mutate的条件语句 [英] Conditional statement combining case_when and mutate

查看:380
本文介绍了结合case_when和mutate的条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个绘制两个变量的二元映射:productionpossession.为了给部分数据正确的颜色,我想为一个变量添加一个色标为"A", "B", "C"的列,为另一个变量1, 2, 3添加一列.然后稍后将两者混为一谈.只是为了使数据像下面的示例一样被编码:

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:

这是我的示例df和失败的代码:

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"))

有了这些结果...:

# 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  

但这是所需的输出:

# 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 

我是新手,将case_when()与mutate结合使用,希望有人可以提供帮助.

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

推荐答案

也许是这样:

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"))

结果:

# 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

唯一的区别是使用了><,尽管在​​您的示例中某些条件没有多大意义.您也不需要最新版本的dplyr中的.$.

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.

这篇关于结合case_when和mutate的条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆