简体   繁体   中英

R: (list) object cannot be coerced to type 'double' error in for loop

I am trying to convert some code with commands from the apply() family into a for loop but when executing the script I get an error when converting to as.numeric() : (list) object cannot be coerced to type 'double' .

The reason why I want to use a for loop is because of the implementation of a progress bar by using txtProgressBar which to my understanding cannot be used in an apply/lapply command. I've found the pbapply package but as I am calling multiple scripts with the source() command which all run with txtProgressBar as progress indicator, I'd like to use it for consistency reasons.

Here is the code I tried to convert into a for loop.

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()
} 

Here is the original code which works by using lapply and apply functions, however txtProgressBar couldn't be implemented.

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)
  })
})

My question : Does anyone see how to suppress the conversion error mentioned above and complete the for loop in order to include the progress bar with txtProgressBar() ?

This at least runs and makes plots.

as.numeric in the code below is expecting a single element or a vector, but i[4] is a list, so you need to i[, 4] it

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%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM