问题
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
回答1:
EDIT: Initialize sampled
as NA_integer_
:
temp_df<-df #temp_pithouse_join used for dynamically created samples
temp_df$sampled <- NA_integer_ #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])){
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]<-sample(temp_df$Start[j]:temp_df$End[j],1)
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]<-sample(temp_df$Start[j]:temp_df$End[j],1)
} #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_integer_
temp_df %>% mutate_if(is.factor, as.character) -> temp_df
}#i for loop
Looking at the first question you had (Sample using start and end values within a loop in R), I am not quite sure why you need mapply
if you are already looping row by row. Why not just something like in this example:
set.seed(1)
df <- structure(list(ID = structure(1:14, .Label = c("a", "b", "c",
"d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"), class = "factor"),
start = c(25L, 36L, 23L, 15L, 21L, 43L, 39L, 27L, 11L, 21L,
28L, 44L, 16L, 25L), end = c(67L, 97L, 85L, 67L, 52L, 72L,
55L, 62L, 99L, 89L, 65L, 58L, 77L, 88L)), class = "data.frame", row.names = c(NA, -14L))
df$sample <- NA
for (row in 1:nrow(df)) {
df$sample[row] <- sample(df$start[row]:df$end[row], 1)
}
df
#> ID start end sample
#> 1 a 25 67 28
#> 2 b 36 97 74
#> 3 c 23 85 23
#> 4 d 15 67 48
#> 5 e 21 52 49
#> 6 f 43 72 65
#> 7 g 39 55 49
#> 8 h 27 62 40
#> 9 i 11 99 92
#> 10 j 21 89 79
#> 11 k 28 65 60
#> 12 l 44 58 48
#> 13 m 16 77 36
#> 14 n 25 88 66
Created on 2020-06-02 by the reprex package (v0.3.0)
If that works, then hopefully you won't have the error associated with mapply
anymore.
回答2:
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.
来源:https://stackoverflow.com/questions/62147310/mapply-error-after-updating-r-and-tidyverse