有没有一种简洁/简单的方法来处理此data.table R代码? [英] Is There A Neat/Simplest Way To This data.table R Code?

查看:61
本文介绍了有没有一种简洁/简单的方法来处理此data.table R代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OECD数据中的STRATUM很长,为简单起见,我使用了这个名称,并希望将其简化为更简短,更精确的命名,如下代码所示。

The STRATUM from OECD data is so long, for simplicity I put this name and would like to simplified it to a more short and precise naming as in the code below.

pisaMas[,`:=`
             (SchoolType = c(ifelse(STRATUM == "National Secondary School", "Public", 
                                    ifelse(STRATUM == "Religious School", "Religious", 
                                           ifelse(STRATUM == "MOE Technical School", "Technical",0)))))]
pisaMas[,table(SchoolType)]

我想知道是否有简单的方法可以使用data.table包来解决此问题。

I would like to know if there are a simple way to this problems, using data.table package.

推荐答案

data.table 的当前开发版本具有新功能 fcase (根据SQL CASE 时的情况进行建模):

Current development version of data.table has new function fcase (modeled after SQL CASE WHEN) for this situation:

pisaMas[ , SchoolType := fcase(
  STRATUM == "National Secondary School", "Public", 
  STRATUM == "Religious School", "Religious", 
  STRATUM == "MOE Technical School", "Technical",
  default = ''
)]
pisaMas[ , table(SchoolType)]

要获取开发版本,请尝试

To get the development version, try

install.packages(
  'data.table', type = 'source',repos = 'http://Rdatatable.github.io/data.table'
)

如果简单安装无效,则可以查看Installation Wiki以获得更多详细信息:

If the simple install doesn't work, you can check the Installation wiki for some more details:

https:// github.com/Rdatatable/data.table/wiki/Installation

您也可以使用查找表解决此问题,有关详细信息,请参见此问答:

You can also solve this with a lookup table, see this Q&A for details:

https://stackoverflow.com/a/36391018/3576984

这篇关于有没有一种简洁/简单的方法来处理此data.table R代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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