简单的if-else代码执行后,R-studio服务器返回错误 [英] R-studio server returns error after simple if-else code execution

查看:1046
本文介绍了简单的if-else代码执行后,R-studio服务器返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正面临这个问题。分析大数据集(大约300万个观测值),我需要将变量从一种格式转换为另一种格式。具体来说,我有几家公司成立的日期,但有两种格式: YYYY MM-DD-YYYY ,或其他可能的情况,即最后4个字符始终与年份有关。

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:


警告消息:1:在doTryCatch(return(expr),name,parentenv,
handler)中:显示列表重绘不完整2:I n
doTryCatch(return(expr),名称,parentenv,处理程序):无效的
图形状态3:在doTryCatch(return(expr),名称,parentenv,
处理程序):无效的图形状态4:在doTryCatch(return(expr),
名称,parentenv,处理程序)中:显示列表重绘不完整5:在
中,doTryCatch(return(expr),名称,parentenv,处理程序):无效
图形状态6:在doTryCatch(return(expr),名称,parentenv,
处理程序中):无效的图形状态

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?

PS该向量当前是字符向量,您认为这会产生影响吗?

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

推荐答案

它可能看起来很奇怪,但我重新运行代码,现在可以正常工作了。我的意思是,仍然给出上述警告,但输出是所需的警告。
我认为了解警告的来源无关紧要,所以谢谢大家!

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!

这篇关于简单的if-else代码执行后,R-studio服务器返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆