0

我怎样才能有效地将两个键列绑定到一个单列中,以保留两个键的字典顺序?我有兴趣将“loc”用作单个(排序)变量

dt = data.table( 
  loc.x = as.integer(c(1, 1, 3, 1, 3, 1)),
  loc.y = as.integer(c(1, 2, 1, 2, 1, 2)),
  value = letters[1:6]
)
setkey(dt, loc.x, loc.y)
4

1 回答 1

0

我不确定你想要什么,但你可以将它们折叠为字符并对它们进行排序,这将给出 1.1、1.2、1.3、...、2.1、2.2、...

> loc.x = as.integer(c(1, 1, 3, 1, 3, 1))
> loc.y = as.integer(c(1, 2, 1, 2, 1, 2))
> x = cbind(loc.x,loc.y)
> sort(apply(x, 1, function(a) paste0(as.character(a), collapse = ".")))
[1] "1.1" "1.2" "1.2" "1.2" "3.1" "3.1"
于 2016-01-14T16:18:05.083 回答