如何使 rjson 的 fromJSON 方法将 JSON NULL 转换为 R NA?

发布于 2024-12-01 08:34:15 字数 62 浏览 2 评论 0原文

我当然可以在 R 中解决这个下游问题,但我认为这比只让 rjson 以某种方式为我做这件事会更混乱。能做到吗?

I could of course solve this downstream in R, but I think that would be messier compared to just get rjson to do it for me somehow. Can it be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热情消退 2024-12-08 08:34:15

两个想法:

  1. 看看 RJSONIO,并使用它的 fromJSON。要查找的参数是 nullValue,您可以将其设置为 NA。很久以前,在做了一些速度测试之后,我从 rjson 切换到了 RJSONIO,它还生成了更具可读性的 JSON。
  2. 考虑将文本作为字符串读取,并使用 gsub() 将“null”替换为“NA”。如果您不熟悉正则表达式,这并不是特别可靠(如果“null”是一些文本的一部分,您最终可能会删除它,因此务必小心)。

Two ideas:

  1. Take a look at RJSONIO instead, and use its fromJSON. The argument to look for is nullValue, which you can set to be NA. I switched from rjson to RJSONIO a long time ago, after doing some speed tests and it also produces somewhat more readable JSON.
  2. Consider reading in the text as a string, and replace 'null' with 'NA' using gsub(). This isn't particularly robust if you aren't familiar with regular expressions (if "null" is part of a bit of text, you could end up dropping it, so it's important to be careful).
维持三分热 2024-12-08 08:34:15

在我看来,rjson 包中的 fromJSON 的所有工作都是在 C 代码中完成的,所以我的猜测是,没有一种简单的方法可以在不改变其行为的情况下改变其行为C 代码本身。您最好在 R 中进行转换。

您可以简单地将 fromJSON 包装在您自己的函数中,将 NULL 替换为 NA。这将防止你的代码本身变得过于混乱。

It looks to me like fromJSON in the rjson package does all its work in C code, so my guess is that there isn't an easy way to alter its behavior without altering the C code itself. You're probably better off doing the conversion in R.

You could simply wrap fromJSON in your own function that replaces NULL to NA. That would prevent your code itself from getting too terribly messy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文