更新R和tidyverse后出现Mapply错误 [英] Mapply error after updating R and tidyverse

查看:55
本文介绍了更新R和tidyverse后出现Mapply错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用几个循环来研究拒绝采样代码.更新R和 tidyverse 后,我发现该代码不再起作用,并显示以下错误:

 错误:分配的数据mapply(...)必须与现有数据兼容.i列"sampled"发生错误.x无法从< integer>到< logical>由于精度下降.*位置:1.运行`rlang :: last_error()`以查看错误发生的位置.另外:警告消息:在seq.default(x,y,na.rm = TRUE)中:多余的参数"na.rm"将被忽略 

该代码以前可以工作,并且与先前的问题相关,并链接到[here] [1].我试图通过使用R(3.6)和 tidyverse (1.3.0)的较旧版本来解决(避免)此问题,但是现在我需要使用一些不兼容的其他软件包.我不希望重新编写整个代码,我希望只需要进行一些调整就可以使其与新版本的R和 tidyverse 一起使用.

编辑对于为该问题提供的初始 df ,我犯了一个错误. ID After_1 After_2 列应包含字母和数字的组合,而不仅仅是数字.示例 df 已更新.

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

df <- dfsourcetemp_df<-df #temp_pithouse_join 用于动态创建的样本temp_df $ sampled<-NA#将样本列空白,因此我可以针对动态检查确定NA.temp_df%>%mutate_if(is.factor,as.character)->temp_df#将字符更改为字符for(i in 1:100){#确定要运行多少次迭代row_list< -as.list(1:nrow(temp_df))q <-0while(length(row_list)!= 0& q< 10){q< -q + 1#确保我们不会陷入无限循环for(j在row_list中){#此循环替换检查值skip_flag< -FALSE#初始化用于检查替换采样的跳过标志for(k in 4:5){#检查地形列if(is.na(temp_df [j,k])){#print("NA break")#打印(i)休息} else if(is.na(as.integer(temp_df [j,k]))== FALSE){#如果它已经是一个整数,那么,一个包含整数的字符向量,我们已经做到了,接下来#print(下一个整数")下一个#print(下一个整数")} else if(temp_df [j,k] =="){#检查空白值#打印(下一个空字符串")temp_df [j,k]< -NA#如果找到空白值,则替换为NA#打印(将空白固定为NA")下一个}else if(is.na(filter(temp_df,ID == as.character(temp_df [j,k]))["sampled"])){跳到最后skip_flag< -TRUE#打印(跳过标志设置")} 别的 {temp_df [j,k]< -as.integer(filter(temp_df,ID == temp_df [j,k])[6])#用这些ID的采样日期替换ID#打印(成功获取检查值")} #如果别的} #k for循环如果(skip_flag==FALSE){row_list< -row_list [row_list!= j]} 别的 {下一个}#采样部分if(skip_flag == FALSE){temp_df [j,6]< -mapply(function(x,y)if(any(is.na(x)|| is.na(y)))不适用样本(seq(x,y,na.rm = TRUE),1),temp_df [j,开始"],temp_df [j,结束"])temp_df [j,7]< -i#标识运行编号if(any(as.numeric(temp_df [j,4:5])> as.numeric(temp_df [j,6]),na.rm = TRUE)){#打印(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)))不适用样本(seq(x,y,na.rm = TRUE),1),temp_df [j,开始"],temp_df [j,结束"])} #尽管temp_df [j,7] = i}#如果}} #j for循环围绕j循环的#while循环包装器如果(i == 1){df2< -temp_df}别的{df2<-rbind(df2,temp_df)}#别的#清空temp_df,为下一次运行做准备temp_df< -dftemp_df $ sampled<-不适用temp_df%>%mutate_if(is.factor,as.character)->temp_df} #i for循环 

这是我将以 dfsource 读取的示例数据:

 结构(列表(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"), 开始 = 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",",",",",",",",",,",,",,",,",,",,",)采样= 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,NA,NA)),类= c("spec_tbl_df","tbl_df","tbl","data.frame"),row.names = c(NA,-30L),spec =结构(list(cols = list(ID = 结构(列表(),类 = c(collector_character",collector")),开始=结构(list(),类= c("collector_double","collector")),结束=结构体(list(),类= c("collector_double","collector")),之后_1 =结构(list(),类= c("collector_character","collector")),之后_2 =结构(list(),类= c("collector_character","collector")),采样=结构体(list(),类= c("collector_logical","collector")))),默认=结构(list(),类= c("collector_guess",收藏家")),跳过= 1),类="col_spec"))```[1]:https://stackoverflow.com/questions/58653809/sample-using-start-and-end-values-within-a-loop-in-r 

解决方案

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

I have been working on a rejection sampling code using several loops. After updating R and tidyverse I found that the code no longer works, displaying the following error:

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

The code worked previously, and is related to a previous question, linked [here][1]. I have tried to work through (avoid) the issue by using older versions of R (3.6) and tidyverse (1.3.0), but now I have some additional packages that I need to use which are incompatible with older versions of R. I'm not looking to rework the entire code, and I hoping that it will only take a few tweaks to get it working with the newer versions of R and tidyverse.

Edit I made a mistake regarding the initial df I provided for this question. Columns ID, After_1, and After_2 should have contained a combination of letters and numbers instead of only numbers. The example df has been updated.

Here is a modified code example that is displaying the same errors as my actual code:

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

And here is the sample data to use which I would read in as 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

解决方案

I want to thank those of you that offered alternate methods to try to deal with this problem. The issue seems to have been caused by an older version of dplyr. I was using dplyr 0.8.3 when I was getting the error, but the code is now working with dplyr 1.0.0.

这篇关于更新R和tidyverse后出现Mapply错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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