R:(列表)对象不能强制在 for 循环中键入错误'double'



我正在尝试将一些带有apply()系列命令的代码转换为for loop但是在执行脚本时,我在转换为as.numeric()时出现错误:(list) object cannot be coerced to type 'double'

我想使用 for 循环的原因是因为通过使用进度条来实现进度条,据我所知,txtProgressBar不能在 apply/lapply 命令中使用。我已经找到了pbapply包,但由于我使用 source() 命令调用多个脚本,这些脚本都以 txtProgressBar 作为进度指示器运行,出于一致性原因,我想使用它。

这是我尝试转换为 for 循环的代码。

xy <- structure(list(NAME = structure(c(2L, 2L, 1L, 1L), .Label = c("CISCO", "JOHN"), class = "factor"), ID = c(41L, 41L, 57L, 57L), X_START_YEAR = c(1965L, 1932L, 1998L, 1956L), Y_START_VALUE = c(960L, -45L, 22L, -570L), X_END_YEAR = c(1968L, 1955L, 2002L, 1970L), Y_END_VALUE = c(960L, -45L, 22L, -570L), LC = structure(c(1L, 1L, 2L, 2L), .Label = c("CA", "US"), class = "factor")), .Names = c("NAME", "ID", "X_START_YEAR","Y_START_VALUE", "X_END_YEAR", "Y_END_VALUE", "LC"), class = "data.frame", row.names = c(NA,-4L))
ind <- split(xy,xy$ID)
# Progress bar
pb = txtProgressBar(min = 0, max = length(ind), initial = 0,title=Test, style=3)
progress <- 1
# Plots
for (i in ind){
  xx = unlist(i[, c(3, 5)])
  yy = unlist(i[, c(4, 6)])   
  fname <- paste0(i[1, 'ID'],'.png')
  png(fname, width=1679, height=1165, res=150)
  par(mar=c(6,8,6,5))
  plot(xx,yy,type='n',main=unique(i[,1]), xlab="Time [Years]", ylab="Value [mm]",ylim = range(c(yy,-.5,.5))) 
  i <- i[,-1]
  rect(i[3], min(i[4], 0), i[5], max(i[4], 0), col=if(as.numeric(i[4]) < 0) 'red' else 'blue')
  abline(h=0, col = "gray60")
  progress = progress + 1
  setTxtProgressBar(pb,progress)
  dev.off()
} 

这是通过使用lapplyapply函数工作的原始代码,但是无法实现txtProgressBar。

xy <- structure(list(NAME = structure(c(2L, 2L, 1L, 1L), .Label = c("CISCO", "JOHN"), class = "factor"), ID = c(41L, 41L, 57L, 57L), X_START_YEAR = c(1965L, 1932L, 1998L, 1956L), Y_START_VALUE = c(960L, -45L, 22L, -570L), X_END_YEAR = c(1968L, 1955L, 2002L, 1970L), Y_END_VALUE = c(960L, -45L, 22L, -570L), LC = structure(c(1L, 1L, 2L, 2L), .Label = c("CA", "US"), class = "factor")), .Names = c("NAME", "ID", "X_START_YEAR","Y_START_VALUE", "X_END_YEAR", "Y_END_VALUE", "LC"), class = "data.frame", row.names = c(NA,-4L))
ind <- split(xy,xy$ID)
lapply(ind, function(x) {
  plot(unlist(x[, c(3, 5)]), unlist(x[, c(4, 6)]), type='n', 
       xlab='Time [Years]', ylab='Value [mm]', main=x[1, 1])
  apply(x, 1, function(y) {
    rect(y[3], min(y[4], 0), y[5], max(y[4], 0), 
         col=if(as.numeric(y[4]) < 0) 'red' else 'blue')
    abline(h=0)
  })
})

我的问题:有没有人看到如何抑制上面提到的转换错误并完成 for 循环以包含带有 txtProgressBar() 的进度条?

至少运行并制作了情节。

下面的代码as.numeric需要单个元素或向量,但i[4]是一个列表,所以你需要i[, 4]

xy <- structure(list(NAME = structure(c(2L, 2L, 1L, 1L), .Label = c("CISCO", "JOHN"), class = "factor"), ID = c(41L, 41L, 57L, 57L), X_START_YEAR = c(1965L, 1932L, 1998L, 1956L), Y_START_VALUE = c(960L, -45L, 22L, -570L), X_END_YEAR = c(1968L, 1955L, 2002L, 1970L), Y_END_VALUE = c(960L, -45L, 22L, -570L), LC = structure(c(1L, 1L, 2L, 2L), .Label = c("CA", "US"), class = "factor")), .Names = c("NAME", "ID", "X_START_YEAR","Y_START_VALUE", "X_END_YEAR", "Y_END_VALUE", "LC"), class = "data.frame", row.names = c(NA,-4L))
ind <- split(xy,xy$ID)
# Progress bar
pb = txtProgressBar(min = 0, max = length(ind), initial = 0,title=Test, style=3)
progress <- 1
# Plots
## changed these next two lines
for (i in seq_along(ind)){
  i <- ind[[i]]

  xx = unlist(i[, c(3, 5)])
  yy = unlist(i[, c(4, 6)])   
  fname <- paste0(i[1, 'ID'],'.png')
#   png(fname, width=1679, height=1165, res=150)
  par(mar=c(6,8,6,5))
  plot(xx,yy,type='n',main=unique(i[,1]), xlab="Time [Years]", ylab="Value [mm]",ylim = range(c(yy,-.5,.5))) 
#   i <- i[,-1]
  apply(i, 1, function(y) 
    rect(y[3], min(y[4], 0), y[5], max(y[4], 0), 
         col=if(as.numeric(y[4]) < 0) 'red' else 'blue'))

  abline(h=0, col = "gray60")
  progress = progress + 1
  setTxtProgressBar(pb,progress)
#   dev.off()
} 

#  |==========================================================================| 100%

最新更新