case_when()不起作用:mutate_impl(.data,点)中的错误

时间:2018-12-11 12:19:49

标签: r dataframe dplyr plyr tidyverse

当我运行下面的代码时,出现此错误:

  

mutate_impl(.data,点)中的错误:列den的长度必须为2   (组大小)或一个,而不是20000

以下是我的示例数据和代码:

mydata<-structure(list(condition = c("A", "B", "C", "D", "E", "A", "B", 
    "C", "D", "E"), r_condition = c(-0.753941712419928, 0.18796391661835, 
    1.07774993391177, 0.141452804751733, -1.67697957194689, -0.730021740287756, 
    0.30088191760836, 0.820982130392851, 0.20992883143208, -1.75797911497123
    ), den_a = c(-1106.36423037457, -1106.36423037457, -1106.36423037457, 
    -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, 
    -1106.36423037457, -1106.36423037457, -1106.36423037457), den_b = c(1013.85560135413, 
    1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 
    1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 
    1013.85560135413), den_c = c(4340.91752722743, 4340.91752722743, 
    4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 
    4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743
    ), den_d = c(1078.95631612632, 1078.95631612632, 1078.95631612632, 
    1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 
    1078.95631612632, 1078.95631612632, 1078.95631612632), den_e = c(-6530.58467431366, 
    -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, 
    -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, 
    -6530.58467431366)), row.names = c(NA, -10L), class = c("grouped_df", 
    "tbl_df", "tbl", "data.frame"), vars = "condition", drop = TRUE, indices = list(
        c(0L, 5L), c(1L, 6L), c(2L, 7L), c(3L, 8L), c(4L, 9L)), group_sizes = c(2L, 
    2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
        condition = c("A", "B", "C", "D", "E")), row.names = c(NA, 
    -5L), class = "data.frame", vars = "condition", drop = TRUE))

mydata%>% mutate(den = case_when (
       mydata$condition=="E" ~ mydata$den_e, 
       mydata$condition=="D" ~ mydata$den_d,
       mydata$condition=="C" ~ mydata$den_c, 
       mydata$condition=="B" ~ mydata$den_b,
       mydata$condition=="A" ~ mydata$den_a))

herehere讨论了类似的问题,但没有一个涉及case_when

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您是否尝试过重新启动R会话?因为对我来说,它似乎可以使用phiver建议的默认dplyr表示法。

library(tidyverse)

mydata<-structure(list(condition = c("A", "B", "C", "D", "E", "A", "B", "C", "D", "E"), r_condition = c(-0.753941712419928, 0.18796391661835, 1.07774993391177, 0.141452804751733, -1.67697957194689, -0.730021740287756, 0.30088191760836, 0.820982130392851, 0.20992883143208, -1.75797911497123), den_a = c(-1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457), den_b = c(1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413), den_c = c(4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743), den_d = c(1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632), den_e = c(-6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366)), row.names = c(NA, -10L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "condition", drop = TRUE, indices = list(c(0L, 5L), c(1L, 6L), c(2L, 7L), c(3L, 8L), c(4L, 9L)), group_sizes = c(2L, 2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(condition = c("A", "B", "C", "D", "E")), row.names = c(NA, -5L), class = "data.frame", vars = "condition", drop = TRUE))

x <- mydata%>% mutate(den = case_when (
  condition=="D" ~ den_d,
  condition=="C" ~ den_c, 
  condition=="E" ~ den_e, 
  condition=="B" ~ den_b,
  condition=="A" ~ den_a))

x
#> # A tibble: 10 x 8
#> # Groups:   condition [5]
#>    condition r_condition  den_a den_b den_c den_d  den_e    den
#>    <chr>           <dbl>  <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>
#>  1 A              -0.754 -1106. 1014. 4341. 1079. -6531. -1106.
#>  2 B               0.188 -1106. 1014. 4341. 1079. -6531.  1014.
#>  3 C               1.08  -1106. 1014. 4341. 1079. -6531.  4341.
#>  4 D               0.141 -1106. 1014. 4341. 1079. -6531.  1079.
#>  5 E              -1.68  -1106. 1014. 4341. 1079. -6531. -6531.
#>  6 A              -0.730 -1106. 1014. 4341. 1079. -6531. -1106.
#>  7 B               0.301 -1106. 1014. 4341. 1079. -6531.  1014.
#>  8 C               0.821 -1106. 1014. 4341. 1079. -6531.  4341.
#>  9 D               0.210 -1106. 1014. 4341. 1079. -6531.  1079.
#> 10 E              -1.76  -1106. 1014. 4341. 1079. -6531. -6531.

reprex package(v0.2.1)于2018-12-11创建

答案 1 :(得分:1)

您只有一些错误,使用此代码,效果很好:

mydata <- mydata%>% mutate(den = case_when (
  condition=="E" ~ den_e, 
  condition=="D" ~ den_d,
  condition=="C" ~ den_c, 
  condition=="B" ~ den_b,
  condition=="A" ~ den_a))