Darren
Darren

Reputation: 71

Why do we get NULL as a side effect in data.table

The data and code below is from the ??data.table and example(data.table).

DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
DT[2:5,cat(v,"\n")]        # just for j's side effect

2 3 4 5 
NULL

I do not understand why we get NULL after cat(v). Are we able to not get NULL?

Upvotes: 0

Views: 69

Answers (1)

akuiper
akuiper

Reputation: 214967

You get NULL because the expression at the j position of data table, i.e., cat returns NULL and thus you get a NULL value returned and since there is no variable to capture the value, it will be printed in the console by default. If you don't want it to get printed, you can assign it to a variable:

x <- DT[2:5, cat(v, "\n")]
# 2 3 4 5 
x
# NULL

Upvotes: 4

Related Questions