the following problem certainly doesn't only apply to the lubridate
package and maybe this has been discussed elsewhere. Unfortunately, I don't know how to ask my question in a more generic way. Therefore here with the lubridate
example:
下面的问题当然不仅仅适用于润滑剂的包装,这可能已经在其他地方讨论过了。不幸的是,我不知道如何用更一般的方式问我的问题。因此,这里以润滑油为例:
I have a database in Excel that includes dates written in Dutch in the following format:
我有一个Excel数据库,其中包含用荷兰语写成的日期,格式如下:
dd/month/yyyy (e.g. 15 Maart 2017)
dd/month/yyyy(例如2017年Maart 15)
Now I can use the dmy()
command from lubridate
with specifying locale = 'Dutch'
on Windows. On a mac in need to specifcy locale = 'nl_NL'
to not receive an error message.
现在,我可以使用来自lubridate的dmy()命令,在Windows上指定locale = 'Dutch'。在mac上,需要指定locale = 'nl_NL'以不接收错误消息。
Since I work on both systems (Windows and mac) interchangeably I am curious to find out if there is a way to avoid changing this code manually. I was thinking of try
or tryCatch
. I could also work with if
statements and specify in earlier lines whether I work on a Windows or mac. But the latter would also require a manual entry from the user which I would like to avoid.
由于我同时使用这两个系统(Windows和mac),我很想知道是否有一种方法可以避免手工修改这些代码。我在考虑尝试或尝试捕捉。我还可以使用if语句,并在前面几行中指定我是在Windows还是mac上工作。但是后者也需要用户手动输入,我不想这样做。
Thanks a lot for you help!
非常感谢你的帮助!
1 个解决方案
#1
3
You mention:
你提到:
But the latter would also require a manual entry from the user
但后者也需要用户手动输入
That is not necessarily true, you could for example do the following:
这并不一定是真的,你可以举个例子:
if(grepl("windows",Sys.info()[['sysname']],ignore.case = T))
{
my_locale='Dutch'
} else
{
my_locale='nl_NL'
}
and then specify:
然后指定:
locale = my_locale
Your code should now work on Windows and Mac. However this will likely fail when you switch to Linux for example. So I wonder if there are better solutions.. Anyway, hope this helps!
您的代码现在应该可以在Windows和Mac上运行,但是当您切换到Linux时,这可能会失败。所以我想知道是否有更好的解决办法。无论如何,希望这可以帮助!
#1
3
You mention:
你提到:
But the latter would also require a manual entry from the user
但后者也需要用户手动输入
That is not necessarily true, you could for example do the following:
这并不一定是真的,你可以举个例子:
if(grepl("windows",Sys.info()[['sysname']],ignore.case = T))
{
my_locale='Dutch'
} else
{
my_locale='nl_NL'
}
and then specify:
然后指定:
locale = my_locale
Your code should now work on Windows and Mac. However this will likely fail when you switch to Linux for example. So I wonder if there are better solutions.. Anyway, hope this helps!
您的代码现在应该可以在Windows和Mac上运行,但是当您切换到Linux时,这可能会失败。所以我想知道是否有更好的解决办法。无论如何,希望这可以帮助!