简体   繁体   English

R数据表setkey - 错误某些列不在data.table中

[英]R Data table setkey - error some columns are not in the data.table

I would like to use data.table setkey with pre-defined lists id and categories , but get an error message: 我想使用带有预定义列表idcategories data.table setkey ,但是会收到一条错误消息:

> setkey(tr_id_cat_dt, id, categories)
Error in setkeyv(x, cols, verbose = verbose) : 
  some columns are not in the data.table: categories

I would like all the elements of id and categories to appear as keys. 我希望idcategories所有元素都显示为键。

Is this possible? 这可能吗?


id and categorie are lists of values, for example: id和categorie是值列表,例如:

categories = c(9115L, 9909L, 3203L, 5558L, 4401L, 1703L, 1726L, 3504L, 3509L, 
5122L, 5616L, 5619L, 2202L, 2119L, 6202L, 5824L, 799L, 4517L, 
7205L, 706L)


dput(head(tr_id_cat_dt))
structure(list(id = c(86246, 86246, 86246, 86246, 86246, 86246
    ), category = c(706L, 706L, 706L, 706L, 706L, 706L)), .Names = c("id", 
    "category"), sorted = c("id", "category"), class = c("data.table", 
    "data.frame"), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x015424a0>)

You can setkey only on the columns of a data.table (as @Roland already pointed out). 您只能在setkey的列上设置data.table (正如@Roland已经指出的那样)。

require(data.table)
DT = data.table(x = 1:2, y=3:4)
z = 5:6
setkey(DT, x) # works
setkey(DT, z) # doesn't
# Error in setkeyv(x, cols, verbose = verbose, physical = physical) : 
#   some columns are not in the data.table: z

setkey(DT[, z := z], z) # works

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM