【问题标题】:R: (list) object cannot be coerced to type 'double' error in for loopR:(列表)对象不能被强制在 for 循环中键入“双”错误
【发布时间】:2014-11-20 02:09:26
【问题描述】:

我正在尝试使用来自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() 中包含进度条?

【问题讨论】:

  • 这与您的问题无关,但您应该考虑在代码中更好地使用空格。谷歌设置了一个很好的R code style standard。它将有助于提高可读性,从而更容易识别逻辑问题。
  • 也许是for (i in seq_along(ind)) { i &lt;- ind[[i]]; etc,剩下的就别管了
  • 但要回答这个问题,在if (as.numeric(y[4]) 中,您试图强制使用列表而不是向量if (as.numeric(y[,4]) 有效

标签: r list for-loop apply


【解决方案1】:

这至少可以运行并制作情节。

下面代码中的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%

【讨论】:

  • @redshoe 查看编辑。创建矩形时,您忘记将 apply 添加到 for 循环中。我刚刚从lapply 循环中复制了它。 apply 函数的好处是输入可以更复杂,例如列表或向量,而 for (i in ) 中的 i 必须是单个数字或字符串(据我所知)
猜你喜欢
  • 2022-06-29
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 1970-01-01
  • 2016-05-24
  • 1970-01-01
相关资源
最近更新 更多