Я работаю над кодом выборки отказа, используя несколько циклов. После обновления 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
ответа на этот вопрос. Столбцы ID
, After_1
и After_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