更新R和tidyverse后出现错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更新R和tidyverse后出现错误相关的知识,希望对你有一定的参考价值。

我一直在使用多个循环来研究拒绝采样代码。更新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

该代码先前有效,并且与先前的问题相关,链接为here。我已尝试通过使用较旧的R(3.6)和tidyverse(1.3.0)解决(避免)该问题,但是现在我需要使用一些与R的较早版本不兼容的其他程序包我不打算重新编写整个代码,我希望它只需要进行一些调整就可以与较新版本的R和tidyverse一起使用。

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

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(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
29, 30), 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(3, NA, NA, NA, 3, NA, NA, NA, NA, NA, NA, 11, 
NA, 11, NA, NA, NA, NA, NA, NA, NA, 21, NA, NA, NA, NA, NA, NA, 
NA, 28), After_2 = c(NA, NA, NA, NA, 2, NA, NA, NA, NA, NA, NA, 
NA, NA, 12, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), 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_double", 
    "collector")), Start = structure(list(), class = c("collector_double", 
    "collector")), End = structure(list(), class = c("collector_double", 
    "collector")), After_1 = structure(list(), class = c("collector_double", 
    "collector")), After_2 = structure(list(), class = c("collector_double", 
    "collector")), sampled = structure(list(), class = c("collector_logical", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

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

答案

[考虑到您遇到的第一个问题(Sample using start and end values within a loop in R),如果您已经逐行循环,我不确定为什么需要mapply。为什么不只是本例中的内容:

以上是关于更新R和tidyverse后出现错误的主要内容,如果未能解决你的问题,请参考以下文章

在R tidyverse中获取感叹号之前的单词

优化 R 中输出表生成的代码

R语言实战应用精讲50篇(三十一)-R语言入门系列-tidyverse数据分析流程

更新 firebase-admin 后出现 EACCES 错误

在下面的代码片段中的剩余 ='passthrough' 处的代码中出现语法错误

怎么处理Win10系统更新提示代码0x80070057的错误?