3

I am currently facing this problem. Analyzing a big data-set (roughly 3 million observations), I need to convert a variable from a format to another. Specifically, I had the date of incorporation of several firms, but coming in two formats: YYYY or MM-DD-YYYY, or other possibilities of which the last 4 characters were always relative to the year.

What I need is just the year so I developed this code:

library(stringi)

for (i in 1:length(amadeus$Dateofincorporation) {
    if(nchar(amadeus$Dateofincorporation[i]) == 4 & 
       !is.na(amadeus$Dateofincorporation[i])) {
        amadeus$Dateofincorporation[i] <- amadeus$Dateofincorporation[i]
    } 
    else if (nchar(amadeus$Dateofincorporation[i]) != 4 & 
             !is.na(amadeus$Dateofincorporation[i])) {
        amadeus$Dateofincorporation[i] <- stri_sub(amadeus$Dateofincorporation[i],-4,-1)
    } 
    else { 
        amadeus$Dateofincorporation[i] <- amadeus$Dateofincorporation[i] 
    }
}

The code executes for a long time, and then returns the output:

Warning messages: 1: In doTryCatch(return(expr), name, parentenv, handler) : display list redraw incomplete 2: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state 3: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state 4: In doTryCatch(return(expr), name, parentenv, handler) : display list redraw incomplete 5: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state 6: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state

Does anyone have an idea on how to deal with this?

P.S. the vector is currently a character vector, do you think this has an impact?

C. Braun
  • 5,061
  • 19
  • 47
Sebastiano
  • 41
  • 1
  • 1
  • 4
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 14 '18 at 14:57
  • If your code is just getting the last four digits, wouldn't this be equivalent? `sub(".*(\\d{4}$)", "\\1", amadeus$Dateofincorporation)` – C. Braun Feb 14 '18 at 15:20
  • @MrFlick sorry and thanks for making me aware. C.Braun yes, indeed your code is one among several possible, equivalent ways of doing it – Sebastiano Feb 14 '18 at 15:35

4 Answers4

3

Just increase the size of the graphics device (bottom right pane in rstudio) and the warning will vanish. Alernatively clear all the plots already in the graphics pane and you won't see the warning again.

Lazarus Thurston
  • 1,197
  • 15
  • 33
2

It may have nothing to do with the code that you have posted. It happens to me every time I have a plot left in the "Plots" tab from previous sessions, even after I have deleted the plot; the warning messages appear after I enter the first line of code

require(package)

1: In doTryCatch(return(expr), name, parentenv, handler) :
  display list redraw incomplete
2: In doTryCatch(return(expr), name, parentenv, handler) :
  invalid graphics state

The second message repeats as many times as the number of plots left in the "Plots" tab.

I usually get rid of these warnings by entering

dev.off()

See https://community.rstudio.com/t/strange-warning-on-initial-rstudio-statement/57003

1

It may look weird, but I re-ran the code, and now it works. I mean, still gives the above warning, but the output is the desired one. I don't think it's relevant to understand the origins of the warnings, so thank you all!

Sebastiano
  • 41
  • 1
  • 1
  • 4
  • If you haven't guessed the answer yet I have posted it now just to complete this Question. If you can please accept the answer the Question will be closed. – Lazarus Thurston Aug 29 '22 at 18:25
0

I think the problem is using the package plyr. I rarely use non base R packages, and as soon as I started using plyr I started getting these bizarre warning messages for seemingly innocent statements, like creating a new vector. I removed plyr from the script and re-ran my code, and all the warning messages disappeared!

notebook
  • 1
  • 2