【问题标题】:Mapply error after updating R and tidyverse更新 R 和 tidyverse 后映射错误
【发布时间】:2020-09-20 15:28:09
【问题描述】:

我一直在使用多个循环编写拒绝采样代码。 更新R和tidyverse后发现代码已经不行了,显示如下错误:

Error: Assigned data `mapply(...)` must be compatible with existing data.
i Error occurred for column `sampled`.
x Can't convert from <integer> to <logical> due to loss of precision.
* Locations: 1.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
In seq.default(x, y, na.rm = TRUE) :
 extra argument ‘na.rm’ will be disregarded

该代码以前有效,并且与上一个问题相关,链接 [此处][1]。 我试图通过使用旧版本的 R (3.6) 和 tidyverse (1.3.0) 来解决(避免)这个问题,但现在我需要使用一些与旧版本的 R 不兼容的附加包. 我不打算重做整个代码,我希望只需要一些调整就可以让它与新版本的 R 和 tidyverse 一起工作。

编辑我在为这个问题提供的初始 df 上犯了一个错误。 IDAfter_1After_2 列应该包含字母和数字的组合,而不仅仅是数字。示例df 已更新。

这是一个修改后的代码示例,显示的错误与我的实际代码相同:

df <- dfsource
temp_df<-df #temp_pithouse_join used for dynamically created samples
temp_df$sampled <- NA #blanking out the sample column so I can check against NA for the dynamic detereminatination.
temp_df %>% mutate_if(is.factor, as.character) -> temp_df #change factors to characters

for (i in 1:100){ #determines how many iterations to run

  row_list<-as.list(1:nrow(temp_df))
  q<-0

  while(length(row_list)!=0 & q<10){
    q<-q+1 #to make sure that we don't spinning off in an infinite loop
    for(j in row_list){ #this loop replaces the check values
      skip_flag<-FALSE #initialize skip flag used to check the replacement sampling
      for(k in 4:5){ #checking the topoafter columns
        if(is.na(temp_df[j,k])){ 
          # print("NA break")
          # print(i)
          break
        } else if(is.na(as.integer(temp_df[j,k]))==FALSE) { #if it's already an integer, well, a character vector containing an integer, we already did this, next
          # print("integer next")
          next
          # print("integer next")
        } else if(temp_df[j,k]==""){ #check for blank values
          # print("empty string next")
          temp_df[j,k]<-NA #if blank value found, replace with NA
          # print("fixed blank to NA")
          next 
        }
        else if(is.na(filter(temp_df,ID==as.character(temp_df[j,k]))["sampled"])) { #if the replacement has not yet been generated, move on, but set flag to jump this to the end
          skip_flag<-TRUE
          # print("skip flag set")
        } else {
          temp_df[j,k]<-as.integer(filter(temp_df,ID==temp_df[j,k])[6]) #replacing IDs with the sampled dates of those IDs
          # print("successful check value grab")
        } #if-else
      } #k for loop
      if(skip_flag==FALSE){
        row_list<-row_list[row_list!=j]
      } else {
        next 
      }

      #sampling section
      if(skip_flag==FALSE){
        temp_df[j,6]<-mapply(function(x, y) if(any(is.na(x) || is.na(y))) NA else 
          sample(seq(x, y, na.rm = TRUE), 1), temp_df[j,"Start"], temp_df[j,"End"])
        temp_df[j,7]<-i #identifying the run number

        if(any(as.numeric(temp_df[j,4:5])>as.numeric(temp_df[j,6]),na.rm=TRUE)){
          # print(j)
          while(any(as.numeric(temp_df[j,4:5])>as.numeric(temp_df[j,6]),na.rm=TRUE)){
            temp_df[j,6]<-mapply(function(x, y) if(any(is.na(x) || is.na(y))) NA else 
              sample(seq(x, y, na.rm = TRUE), 1), temp_df[j,"Start"], temp_df[j,"End"])
          } #while 
          temp_df[j,7]=i 
        }#if
      }
    } #j for loop
  } #while loop wrapper around j loop
  if(i==1){
    df2<-temp_df
  }else{
    df2<-rbind(df2,temp_df)
  }#else

  #blank out temp_df to prepare for another run
  temp_df<-df
  temp_df$sampled <- NA 
  temp_df %>% mutate_if(is.factor, as.character) -> temp_df 

}#i for loop

这里是要使用的示例数据,我会读为dfsource

structure(list(ID = c("A1", "A2", "A3", "A4", "A5", "A6", "A7", 
"A8", "A9", "A10", "A11", "A12", "A13", "A14", "A15", "A16", 
"A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", 
"A26", "A27", "A28", "A29", "A30"), Start = c(1, 1, 1, 1, 1, 
50, 50, 50, 50, 50, 100, 100, 100, 100, 100, 200, 200, 300, 250, 
350, 300, 300, 400, 500, 400, 400, 450, 500, 550, 500), End = c(1000, 
1000, 1000, 1000, 1000, 950, 950, 950, 950, 950, 1000, 1000, 
1000, 1000, 900, 800, 900, 750, 650, 650, 600, 850, 700, 600, 
600, 700, 550, 550, 600, 550), After_1 = c("A3", "", "", "", 
"A3", "", "", "", "", "", "", "A11", "", "A11", "", "", "", "", 
"", "", "", "A21", "", "", "", "", "", "", "", "A28"), After_2 = c("", 
"", "", "", "A2", "", "", "", "", "", "", "", "", "A12", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", ""), 
    sampled = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -30L), spec = structure(list(cols = list(
    ID = structure(list(), class = c("collector_character", "collector"
    )), Start = structure(list(), class = c("collector_double", 
    "collector")), End = structure(list(), class = c("collector_double", 
    "collector")), After_1 = structure(list(), class = c("collector_character", 
    "collector")), After_2 = structure(list(), class = c("collector_character", 
    "collector")), sampled = structure(list(), class = c("collector_logical", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec"))```





  [1]: https://stackoverflow.com/questions/58653809/sample-using-start-and-end-values-within-a-loop-in-r

【问题讨论】:

  • R 3.6。不应该被认为是“老”。 R 4.0 尚未发布第一个补丁。在将 R 3.6 称为“较旧”之前,我们应该至少等到 4.0.1。
  • 为了以后的分析,我正在使用 spearman 的 rho 探索相关性,并希望包括置信区间。 RVAideMemoire 具有此功能,但不幸的是我认为它需要 R 4.0。

标签: r loops tidyverse updates mapply


【解决方案1】:

编辑:将sampled 初始化为NA_integer_

temp_df<-df #temp_pithouse_join used for dynamically created samples
temp_df$sampled <- NA_integer_ #blanking out the sample column so I can check against NA for the dynamic detereminatination.
temp_df %>% mutate_if(is.factor, as.character) -> temp_df #change factors to characters

for (i in 1:100){ #determines how many iterations to run

    row_list<-as.list(1:nrow(temp_df))
    q<-0

    while(length(row_list)!=0 & q<10){
        q<-q+1 #to make sure that we don't spinning off in an infinite loop
        for(j in row_list){ #this loop replaces the check values
            skip_flag<-FALSE #initialize skip flag used to check the replacement sampling
            for(k in 4:5){ #checking the topoafter columns
                if(is.na(temp_df[j,k])){ 
                    break
                } else if(is.na(as.integer(temp_df[j,k]))==FALSE) { #if it's already an integer, well, a character vector containing an integer, we already did this, next
                    # print("integer next")
                    next
                    # print("integer next")
                } else if(temp_df[j,k]==""){ #check for blank values
                    # print("empty string next")
                    temp_df[j,k]<-NA #if blank value found, replace with NA
                    # print("fixed blank to NA")
                    next 
                }
                else if(is.na(filter(temp_df,ID==as.character(temp_df[j,k]))["sampled"])) { #if the replacement has not yet been generated, move on, but set flag to jump this to the end
                    skip_flag<-TRUE
                    # print("skip flag set")
                } else {
                    temp_df[j,k]<-as.integer(filter(temp_df,ID==temp_df[j,k])[6]) #replacing IDs with the sampled dates of those IDs
                    # print("successful check value grab")
                } #if-else
            } #k for loop
            if(skip_flag==FALSE){
                row_list<-row_list[row_list!=j]
            } else {
                next 
            }
            #sampling section
            if(skip_flag==FALSE){
                temp_df[j,6]<-sample(temp_df$Start[j]:temp_df$End[j],1)
                temp_df[j,7]<-i #identifying the run number

                if(any(as.numeric(temp_df[j,4:5])>as.numeric(temp_df[j,6]),na.rm=TRUE)){
                    # print(j)
                    while(any(as.numeric(temp_df[j,4:5])>as.numeric(temp_df[j,6]),na.rm=TRUE)){
                        temp_df[j,6]<-sample(temp_df$Start[j]:temp_df$End[j],1)
                    } #while 
                    temp_df[j,7]=i 
                }#if
            }
        } #j for loop
    } #while loop wrapper around j loop
    if(i==1){
        df2<-temp_df
    }else{
        df2<-rbind(df2,temp_df)
    }#else

    #blank out temp_df to prepare for another run
    temp_df<-df
    temp_df$sampled <- NA_integer_
    temp_df %>% mutate_if(is.factor, as.character) -> temp_df 

}#i for loop

查看您的第一个问题 (Sample using start and end values within a loop in R),如果您已经逐行循环,我不太确定为什么需要 mapply。为什么不只是像这个例子中的那样:

set.seed(1)
df <- structure(list(ID = structure(1:14, .Label = c("a", "b", "c", 
                                                                                                         "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"), class = "factor"), 
                                         start = c(25L, 36L, 23L, 15L, 21L, 43L, 39L, 27L, 11L, 21L, 
                                                            28L, 44L, 16L, 25L), end = c(67L, 97L, 85L, 67L, 52L, 72L, 
                                                                                                                     55L, 62L, 99L, 89L, 65L, 58L, 77L, 88L)), class = "data.frame", row.names = c(NA, -14L))

df$sample <- NA

for (row in 1:nrow(df)) {
    df$sample[row] <- sample(df$start[row]:df$end[row], 1)
}

df
#>    ID start end sample
#> 1   a    25  67     28
#> 2   b    36  97     74
#> 3   c    23  85     23
#> 4   d    15  67     48
#> 5   e    21  52     49
#> 6   f    43  72     65
#> 7   g    39  55     49
#> 8   h    27  62     40
#> 9   i    11  99     92
#> 10  j    21  89     79
#> 11  k    28  65     60
#> 12  l    44  58     48
#> 13  m    16  77     36
#> 14  n    25  88     66

由 reprex 包于 2020-06-02 创建 (v0.3.0)

如果可行,那么希望您不会再遇到与 mapply 相关的错误。

【讨论】:

  • 我尝试通过将采样部分替换为以下内容来集成您的解决方案: if(skip_flag==FALSE){ temp_df[j,6]as.numeric(temp_df[j,6]),na .rm=TRUE)){ # print(j) while(any(as.numeric(temp_df[j,4:5])>as.numeric(temp_df[j,6]),na.rm=TRUE)){ temp_df[j,6] 类似的 错误
  • 查看编辑。将列sampled 初始化为NA_integer_ 的问题是因为NA 默认属于logical 类,因此当您对值进行采样并尝试将整数分配给logical 类的列时会出现问题。另外,请注意,您还需要在注释#blank out temp_df to prepare for another run 之后在代码末尾使用NA_integer_
  • 我兴奋地进入了您的修复程序,然后意识到我的可重现示例有一个明显的错误。 ID、After_1 和 After_2 列包含字母和数字的组合,而不仅仅是数字。对于这次失误,我感到非常抱歉,我现在正在更新示例 df。您的解决方案在我最初提供的 df 上效果很好,但我搞砸了这个例子。我很抱歉。
  • 嗨@Corey,这会对sampled 的问题产生影响吗?
  • 非常感谢您在这里的帮助,我很抱歉混淆了。使用新数据运行时出现以下错误:错误:分配的数据as.integer(filter(temp_df, ID == temp_df[j, k])[6]) 必须与现有数据兼容。 i 列 After_1 发生错误。 x 无法将 转换为 。运行rlang::last_error() 以查看错误发生的位置。另外:警告消息:1:在 as.integer(temp_df[j, k]) 中:强制引入的 NAs
【解决方案2】:

我要感谢那些提供替代方法来尝试解决此问题的人。该问题似乎是由旧版本的 dplyr 引起的。当我收到错误时,我正在使用 dplyr 0.8.3,但代码现在正在使用 dplyr 1.0.0。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 2015-11-04
    • 2021-06-20
    相关资源
    最近更新 更多