我收到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"))
TA,错误消息表明,问题至少发生在is.na(filter(dftemp,ID==dftemp[j,k])[6])
行。问题似乎是dplyr的filter
想要输入什么。考虑以下调用返回的内容:
#returns a tibble with one value
str(dftemp[8,4])
#returns an empty tibble
filter(dftemp,ID==dftemp[8,4])
#returns True
is.data.frame(filter(dftemp,ID==dftemp[8,4]))
过滤器直接需要该值,而不是包含该值的数据框。在对一个值进行过滤时添加as.character
应该可以解决此问题。每当对一个值进行过滤时,都应该发生这种情况,并确保使用正确的数据类型(即在需要时使用“ as.numric”)。下面是一个示例:
#replace line in qusestion with following:
is.na(filter(dftemp,ID==as.character(dftemp[8,4]) )[6])
#testing
if(is.na(filter(dftemp,ID==as.character(dftemp[8,4]) )[6])){print("working")}
#output
[1] "working"