【发布时间】:2019-11-02 11:33:57
【问题描述】:
我收到一个argument is of length zero 错误,我无法找出原因。
此代码应该从一系列数字start 和end 中采样。一些行具有额外的依赖关系,其中某些 ID 需要位于其他 ID 之后。代码通过将列after1 和after2 中的值替换为相应的采样值来检查这些依赖关系。如果不满足所需的依赖关系,则重新采样这些值。
当代码成功运行时,sampled 值应填充满足所需依赖关系的数字。代码针对i 迭代运行,在末尾添加一列表示采样值对应的运行。
我最近调整了在将数据输入 R 之前清理和准备数据的方式。我认为我已经正确转置了代码,但是我收到了旧版本中不存在的以下错误,并且我'不知道如何解决它。我查看了其他帖子,但没有找到适用的解决方案。
Error in if (is.na(filter(dftemp, ID == dftemp[j, k])[6])) { :
argument is of length zero
In addition: Warning message:
In as.integer(dftemp[j, k]) : NAs introduced by coercion
这是我目前正在处理的代码:
df <- read_csv("sampling sample set.csv", na = c("#VALUE!", "#N/A", ""))
dftemp <- df
dftemp %>% mutate_if(is.factor, as.character) -> dftemp #change factors to characters
for (i in 1:200){ #determines how many iterations to run
row_list<-as.list(1:nrow(dftemp))
q<-0
while(length(row_list)!=0 & q<10){
q<-q+1
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 after columns
if(is.na(dftemp[j,k])){
print("NA break")
print(i)
break
} else if(is.na(as.integer(dftemp[j,k]))==FALSE) { #if it's already an integer, we already did this, next
print("integer next")
next
print("integer next")
} else if(dftemp[j,k]==""){ #check for blank values
print("empty string next")
dftemp[j,k]<-NA #if blank value found, replace with NA
print("fixed blank to NA")
next
} else if(is.na(filter(dftemp,ID==dftemp[j,k])[6])) { #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 {
dftemp[j,k]<-as.integer(filter(dftemp,ID==dftemp[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){
dftemp[j,6] <- mapply(function(x, y) sample(seq(x, y), 1), dftemp[j,"start"], dftemp[j,"end"])
dftemp[j,7]<-i #identifying the run number
if(any(as.numeric(dftemp[j,4:5])>as.numeric(dftemp[j,6]),na.rm=TRUE)){
print(j)
while(any(as.numeric(dftemp[j,4:5])>as.numeric(dftemp[j,6]),na.rm=TRUE)){
dftemp[j,6] <- mapply(function(x, y) sample(seq(x, y), 1), dftemp[j,"start"], dftemp[j,"end"])
} #while
dftemp[j,7]=i
}#if
}
} #j for loop
} #while loop wrapper around j loop
if(i==1){
dftemp2<-dftemp
}else{
dftemp2<-rbind(dftemp2,dftemp)
}#else
#blank out dftemp to prepare for another run
dftemp<-dftemp
dftemp$sampled <- NA
dftemp %>% mutate_if(is.factor, as.character) -> dftemp
}#i for loop
这里是示例数据。
structure(list(ID = c("a123-1", "b123-1", "c123-1", "d123-1",
"e123-1", "f123-1", "g123-1", "h123-1", "i123-1", "j123-1", "k123-1",
"l123-1", "m123-1", "n123-1"), start = c(-5100, -4760, -4930,
-4930, -5380, -5280, -4855, -4855, -4855, -4855, -4855, -4855,
-4810, -4810), end = c(-4760, -4420, -4420, -4420, -5080, -5080,
-4750, -4750, -4750, -4750, -4750, -4750, -4710, -4710), after1 = c(NA,
NA, NA, NA, NA, NA, NA, "g123-1", "g123-1", NA, "j123-1", "j123-1",
NA, NA), after2 = c(NA, NA, NA, NA, NA, NA, NA, NA, "h123-1",
NA, NA, "k123-1", NA, NA), sampled = c(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, -14L), 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")), after1 = structure(list(), class = c("collector_character",
"collector")), after2 = 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"))
【问题讨论】:
标签: r loops for-loop if-statement sampling