R-studio server returns error after simple if-else code execution

12,016

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!

Share:
12,016

Related videos on Youtube

Sebastiano
Author by

Sebastiano

PhD econ student at URV, Spain. Interested in economics of innovation, machine learning and methodology of science

Updated on June 04, 2022

Comments

  • Sebastiano
    Sebastiano almost 2 years

    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?

    • MrFlick
      MrFlick about 6 years
      When asking for help, you should include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions.
    • C. Braun
      C. Braun about 6 years
      If your code is just getting the last four digits, wouldn't this be equivalent? sub(".*(\\d{4}$)", "\\1", amadeus$Dateofincorporation)
    • Sebastiano
      Sebastiano about 6 years
      @MrFlick sorry and thanks for making me aware. C.Braun yes, indeed your code is one among several possible, equivalent ways of doing it
  • Lazarus Thurston
    Lazarus Thurston over 5 years
    The warning is still troubling us all and it will be good if can find the reason where it is emanating from. I am on data.table 1.11.4 and the warning still appears erratically i.e. a non-repeatable way.