返回数据表中每个组的多行。

huangapple go评论100阅读模式
英文:

Return multiple rows per group in data.table

问题

可以通过在 data.table 中使用以下代码来实现类似 reframe 的功能:

  1. library(data.table)
  2. dt[, .(x = setdiff(x, y)), by = g]
英文:

Is it possible to return multiple rows per group in a grouped command in data.table? In dplyr, this is done with reframe:

  1. y <- c("a", "b", "d", "f")
  2. df <- tibble(
  3. g = c(1, 1, 1, 2, 2, 2, 2),
  4. x = c("e", "a", "b", "e", "f", "c", "a")
  5. )
  6. library(dplyr)
  7. df %>%
  8. reframe(x = setdiff(x, y), .by = g)
  9. # g x
  10. # 1 e
  11. # 2 e
  12. # 2 c

In data.table, this returns an error:

  1. library(data.table)
  2. dt <- setDT(df)
  3. dt[, x := setdiff(x, y), g]

> Error in [.data.table(df, , :=(x, intersect(x, y)), g) :
> Supplied 2 items to be assigned to group 1 of size 3 in column 'x'.
> The RHS length must either be 1 (single values are ok) or match the
> LHS length exactly. If you wish to 'recycle' the RHS please use rep()
> explicitly to make this intent clear to readers of your code.

Anyway to get a data.table equivalent of reframe?

答案1

得分: 6

Wrap in .(...) 并且在 .(..) 内部使用 = 替代 :=

  1. as.data.table(df)[, .(x = setdiff(x, y)), by = g]
  2. # g x
  3. # <num> <char>
  4. # 1: 1 e
  5. # 2: 2 e
  6. # 3: 2 c

请注意,在底层,.(.) 实际上就是 list(.),所以我们也可以使用任何返回类似 list 的对象的方法,包括:

  1. as.data.table(df)[, list(x = setdiff(x, y)), by = g]
  2. as.data.table(df)[, data.table(x = setdiff(x, y)), by = g]
  3. as.data.table(df)[, data.frame(x = setdiff(x, y)), by = g]
英文:

Wrap in .(...) and use = in place of := (because it's within .(..)).

  1. as.data.table(df)[, .(x = setdiff(x, y)), by = g]
  2. # g x
  3. # <num> <char>
  4. # 1: 1 e
  5. # 2: 2 e
  6. # 3: 2 c

Note that under the hood, .(.) is really just list(.), so we could also use anything that returns list-like objects, including:

  1. as.data.table(df)[, list(x = setdiff(x, y)), by = g]
  2. as.data.table(df)[, data.table(x = setdiff(x, y)), by = g]
  3. as.data.table(df)[, data.frame(x = setdiff(x, y)), by = g]

huangapple
  • 本文由 发表于 2023年5月18日 00:12:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定