本文介绍了R数据表setkey - 错误一些列不在data.table中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用data.table setkey
与预定义的列表 id
和 categories
,但会收到错误消息:
setkey(tr_id_cat_dt,id,categories)
在setkeyv(x,cols,verbose = verbose)中出错:
一些列不在data.table中:categories
我想要 id
和
$ b $ b
id和类别是值列表,例如:
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))
结构(列表(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>)
解决方案
只能在 setkey > data.table (如@Roland已经指出)。
require(data.table)
DT = data.table(x = 1:2,y = 3:4)
z = 5:6
setkey(DT,x)#works
setkey z)#does not
#setkeyv中的错误(x,cols,verbose = verbose,physical = physical):
#有些列不在data.table中:z
setkey(DT [,z:= z],z)#works
HTH
I would like to use data.table setkey
with pre-defined lists id
and categories
, but get an error message:
> 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.
Is this possible?
id and categorie are lists of values, for example:
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).
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
这篇关于R数据表setkey - 错误一些列不在data.table中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!