data.table objects assigned with := from within function not printed

前端 未结 2 761
南笙
南笙 2020-11-22 12:42

I would like to modify a data.table within a function. If I use the := feature within the function, the result is only printed for the second call.

2条回答
  •  甜味超标
    2020-11-22 12:53

    As David Arenburg mentions in a comment, the answer can be found here. There was a bug fixed in the version 1.9.6 but the fix introduced this downside.

    One should call DT[] at the end of the function to prevent this behaviour.

    myfunction <- function(dt) {
        dt[, z := y - x][]
    }
    myfunction(mydt)  # prints immediately
    #    x y z
    # 1: 1 5 4
    # 2: 2 6 4
    # 3: 3 7 4 
    

提交回复
热议问题