Using a “List” in the Data.table Argument j

I am exploring the properties of data.table from a blog post . I am trying to understand the part under the “pivot table (short and narrow)”, starting from forcing data.frame (mtcars) to data.table:

> data <- as.data.table(mtcars)

> data <- data[,.(gear,cyl)]
> head(data)
    gear cyl
 1:    4   6
 2:    4   6
 3:    4   4
 4:    3   6
 5:    3   8
 6:    3   6

Up to this point, everything is fine.

Now i tried this data[, gearsL := list(list(unique(gear))), by=cyl]

> head(data)
   gear cyl gearsL
1:    4   6  4,3,5
2:    4   6  4,3,5
3:    4   4  4,3,5
4:    3   6  4,3,5
5:    3   8    3,5
6:    3   6  4,3,5

I can understand the unique (mechanism) , but I can not understand what the list does (list (unique (transfer)) .

+4
source share
1 answer

Data.table - like any data.frame - this is a list of pointers to column vectors.

When creating new columns, we write jfrom DT[i,j,by]so that it evaluates the list of columns:

DT[, (newcol_names) := list(newcol_A, newcol_B)]

, list() OP, list.

data[,gearsL := list(list(unique(gear))), by=cyl]

.() :

data[, gearsL := .(list(unique(gear))), by=cyl]

, , .


. list()/.():

DT = data.table(id=1:3)
DT[, E := c(4,5,6)]
DT[, R := 3]
# this works as if we had typed
# R := c(3,3,3)

, E , R . :

DT[, Elist := list(hist(rpois(1,1)), hist(rpois(2,2)), hist(rpois(3,3)))]

E, Elist . ; list() , list,

sapply(DT, class)
#        id         E         R     Elist 
# "integer" "numeric" "numeric"    "list" 

list()/.() : list, :

DT[, Rlist := list(c("a","b"))]
# based on the pattern for column R, this should work as if we typed 
# Rlist := list(c("a","b"), c("a","b"), c("a","b"))

, C2 := .( c("a", "b") ) , , Elist. , list()/.():

DT[, Rlist := .(list(c("a","b")))]

#    id E R       Elist Rlist
# 1:  1 4 3 <histogram>   a,b
# 2:  2 5 3 <histogram>   a,b
# 3:  3 6 3 <histogram>   a,b

OP, list()/.().

+8

Source: https://habr.com/ru/post/1611506/


All Articles