更新 R 和 tidyverse 后出现 Mapply 错误
Mapply error after updating R and 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
该代码之前有效,并且与之前的问题相关,链接[此处][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]:
编辑:将 sampled
初始化为 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
看看你的第一个问题 (),如果你已经在逐行循环,我不太确定你为什么需要 mapply
。为什么不只是这个例子中的东西:
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
由 reprex 包 (v0.3.0) 创建于 2020-06-02
如果可行,那么希望您不会再遇到与 mapply
相关的错误。
我要感谢那些提供替代方法来尝试处理此问题的人。这个问题似乎是由旧版本的 dplyr 引起的。当我收到错误时,我正在使用 dplyr 0.8.3,但代码现在可以使用 dplyr 1.0.0。
我一直在使用多个循环编写拒绝采样代码。
更新 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]:
编辑:将 sampled
初始化为 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
看看你的第一个问题 (mapply
。为什么不只是这个例子中的东西:
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
由 reprex 包 (v0.3.0) 创建于 2020-06-02
如果可行,那么希望您不会再遇到与 mapply
相关的错误。
我要感谢那些提供替代方法来尝试处理此问题的人。这个问题似乎是由旧版本的 dplyr 引起的。当我收到错误时,我正在使用 dplyr 0.8.3,但代码现在可以使用 dplyr 1.0.0。