简体   繁体   English

使用 mutate、if_else 和 replace 我得到 In x[list] <- values: 要替换的项目数不是替换长度的倍数

[英]Using mutate, if_else and replace I get In x[list] <- values : number of items to replace is not a multiple of replacement length

I know this subject has been covered several times in SO but I could not find an answer to my problem.我知道这个主题已经在 SO 中多次讨论过,但我找不到我的问题的答案。

I have the following data (edited, added the full real data):我有以下数据(编辑,添加了完整的真实数据):

structure(list(Expo = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), SYN = c("S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U", "N", "S", "U", "N", "S", "U", "N", 
    "S", "U", "N", "S", "U"), freq.n = c(19L, 6L, 1L, 4L, 2L, 
    3L, 2L, 1L, 12L, 10L, 1L, 1L, 1L, 1L, 24L, 4L, 1L, 1L, 3L, 
    1L, 1L, 8L, 49L, 9L, 2L, 3L, 6L, 3L, 2L, 1L, 12L, 7L, 2L, 
    1L, 2L, 1L, 1L, 2L, 1L, 2L, 38L, 3L, 3L, 1L, 3L, 1L, 1L, 
    2L, 4L, 2L, 105L, 7L, 4L, 3L, 1L, 1L, 2L, 5L, 11L, 21L, 2L, 
    1L, 2L, 1L, 1L, 2L, 3L, 67L, 5L, 2L, 6L, 4L, 1L, 1L, 9L, 
    4L, 97L, 19L, 10L, 3L, 6L, 1L, 2L, 2L, 12L, 12L, 2L, 1L, 
    1L, 2L, 2L, 4L, 95L, 14L, 8L, 2L, 2L, 1L, 1L, 4L, 5L, 153L, 
    56L, 13L, 10L, 2L, 4L, 3L, 5L, 4L, 12L, 12L, 1L, 2L, 1L, 
    2L, 1L, 2L, 82L, 33L, 10L, 6L, 3L, 2L, 2L, 2L, 4L, 6L, 82L, 
    3L, 2L, 1L, 3L, 1L, 2L, 3L, 20L, 5L, 3L, 30L, 7L, 5L, 3L, 
    2L, 1L, 2L, 1L, 66L, 10L, 4L, 1L, 1L, 1L, 2L, 23L, 1L, 3L, 
    5L, 3L, 53L, 11L, 3L, 1L, 108L, 18L, 10L, 3L, 1L, 1L, 2L, 
    3L, 14L, 1L, 1L, 2L, 2L, 3L, 2L, 101L, 14L, 4L, 1L, 122L, 
    33L, 9L, 1L, 1L, 2L, 3L, 2L, 3L, 9L, 10L, 2L, 1L, 2L, 1L, 
    1L, 1L, 65L, 20L, 2L, 5L, 2L, 1L, 3L, 2L, 1L, 3L, 11L, 3L, 
    3L, 1L, 1L, 4L, 2L, 1L, 1L, 1L, 6L, 2L, 2L, 1L, 1L)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -224L))

I ran the following code:我运行了以下代码:

library(dplyr)

q = c("3" = "1",
      "5" = "3",
      "7" = "5",
      "9" = "7",
      "10" = "8")

Pi %>% 
mutate(day = if_else(Expo == "DC", 
                     replace(day, day == day, q), as.character(day)))

I get this warning In x[list] <- values: number of items to replace is not a multiple of replacement length with the following result.在 x[list] <- values: number of items to replace is not a multiple of replacement length 中收到此警告,结果如下。

After edition of the question: A subsetted example of what I get.问题编辑后:我得到的一个子集示例。

# A tibble: 6 x 6
  Expo  day   SYN   freq.n
  <fct> <chr> <chr>  <int>
1 DC    1     S         19
2 DC    3     U          6
3 DC    5     N          1
4 DC    7     S          4
5 DC    8     U          2
6 DC    1     N          3

It is clearly assigning incorrect values in "day".它显然在“天”中分配了不正确的值。

The expected output should be:预期的 output 应该是:

# A tibble: 6 x 6
  Expo  day   SYN   freq.n 
  <fct> <chr> <chr>  <int>
1 DC    1     S         19
2 DC    1     U          6 
3 DC    1     N          1 
4 DC    1     S          4 
5 DC    1     U          2 
6 DC    1     N          3 

The thing is that the very same line of code runs perfectly with a similar tbl.问题是相同的代码行与类似的 tbl 完美运行。

I would appreciate some comment that can point me in the right direction.我将不胜感激一些可以为我指明正确方向的评论。

Many thanks in advance.提前谢谢了。

Use dplyr::case_when ... does this do what you want?使用dplyr::case_when ...这是否符合您的要求?

case_when allows for multiple conditions to be applied to a vector, in this case there on only one condition so case_when works as an if else condition: case_when 允许将多个条件应用于向量,在这种情况下,只有一个条件,因此 case_when 用作 if else 条件:

The condition is defined by Expo == "DC" when it is TRUE the formula tilde "~" instigates what to do, which is to replace the value for day from a named vector.条件由Expo == "DC"定义,当它为 TRUE 时,公式波浪号“~”指示要做什么,即从命名向量中替换day的值。 The second part of case_when ie the TRUE ~ as.character(day) mops up the unresolved elements of the vector, it can be treated as the "else" part of and if else conditional ie it deals with all the cases where (counter intuitively) the result of the condition is FALSE. case_when 的第二部分,即TRUE ~ as.character(day)向量的未解析元素,它可以被视为 if else 条件的“else”部分,即它处理所有情况(直观地反) 条件的结果为 FALSE。 Another way of looking at it is to say all the elements not affected by the previous case_when are TRUE and the ~ lets R know what to do with them.另一种看待它的方法是说所有不受前面 case_when 影响的元素都是 TRUE 并且 ~ 让 R 知道如何处理它们。


library(dplyr)
library(stringr)

df %>% 
  mutate(day = case_when(Expo == "DC" ~ str_replace_all(day, q),
                         TRUE ~ as.character(day))) %>% 
  head(20)
#> # A tibble: 20 x 4
#>    Expo  day   SYN   freq.n
#>    <fct> <chr> <chr>  <int>
#>  1 DC    1     S         19
#>  2 DC    1     U          6
#>  3 DC    1     N          1
#>  4 DC    1     S          4
#>  5 DC    1     U          2
#>  6 DC    1     N          3
#>  7 DC    1     S          2
#>  8 DC    1     U          1
#>  9 DC    1     N         12
#> 10 DC    1     S         10
#> 11 DC    1     U          1
#> 12 DC    1     N          1
#> 13 DC    1     S          1
#> 14 DC    1     U          1
#> 15 DC    1     N         24
#> 16 DC    1     S          4
#> 17 DC    1     U          1
#> 18 DC    1     N          1
#> 19 DC    1     S          3
#> 20 DC    1     U          1

Created on 2020-07-10 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 7 月 10 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我要“替换的项目数量不是替换长度的倍数” - Why do I get “number of items to replace is not a multiple of replacement length” 替换向量中的值,要替换的项数不是替换长度的倍数 - replacing values in vector, number of items to replace is not a multiple of replacement length R要替换的项目数不是创建列表的替换长度的倍数 - R number of items to replace is not a multiple of replacement length for creating list 要替换的项目数不是替换长度的倍数-Datacamp - Number of items to replace is not a multiple of replacement length - Datacamp 要替换的项目数不是替换长度错误的倍数? - number of items to replace is not a multiple of replacement length error? R要更换的物品数量不是更换长度的倍数 - R number of items to replace is not a multiple of replacement length 错误:要替换的项目数不是替换长度的倍数 - Error: number of items to replace is not a multiple of replacement length 警告:要替换的项目数不是替换长度的倍数 - Warning: number of items to replace is not a multiple of replacement length 要替换的项目数不是替换长度的倍数 - Number of items to be replace is not a multiple of replacement length x $ data [i,j]中的错误&lt;-值:要替换的项目数不是替换长度的倍数 - Error in x$data[i, j] <- value : number of items to replace is not a multiple of replacement length
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM