有没有一种简洁/简单的方法来处理此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://*.com/a/36391018/3576984