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

妖精的绣舞 提交于 2019-12-10 10:36:39

问题


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?


回答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!



来源:https://stackoverflow.com/questions/48789420/r-studio-server-returns-error-after-simple-if-else-code-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!