简体   繁体   English

如何返回在单元格中包含长度大于1的列表或向量的data.tables?

[英]How do I return data.tables that contain lists or vectors of length greater than 1 in a cell?

I'd like to specify in j that I want to return full lists in a given column. 我想在j中指定要在给定列中返回完整列表。 Below, when I try to return lists in column c , it expands it so that each cell in c is a single item. 在下面,当我尝试返回c列中的列表时,它会对其进行扩展,以便c中的每个单元格都是单个项目。 I actually want there to be 4 rows returned, with every row in c showing the list list('testing1', 'testing2', 'testing3', 'testing4') . 我实际上希望返回4行,而c每一行都显示列表list('testing1', 'testing2', 'testing3', 'testing4')

> x <- data.table(a=1:4, b=5:8)
> x[, list(b, c=list('testing1', 'testing2', 'testing3', 'testing4')), by=a]
    a b        c
 1: 1 5 testing1
 2: 1 5 testing2
 3: 1 5 testing3
 4: 1 5 testing4
 5: 2 6 testing1
 6: 2 6 testing2
 7: 2 6 testing3
 8: 2 6 testing4
 9: 3 7 testing1
10: 3 7 testing2
11: 3 7 testing3
12: 3 7 testing4
13: 4 8 testing1
14: 4 8 testing2
15: 4 8 testing3
16: 4 8 testing4

You say you want this: 您说您想要这个:

y <- x[, list(b, c=list(list('testing1', 'testing2', 'testing3', 'testing4'))), by=a]
y[1,c]
# [[1]]
# [[1]][[1]]
# [1] "testing1"
# 
# [[1]][[2]]
# [1] "testing2"
# 
# [[1]][[3]]
# [1] "testing3"
# 
# [[1]][[4]]
# [1] "testing4"

But I suspect you want this: 但是我怀疑你想要这个:

x[, list(b, c=list(c('testing1', 'testing2', 'testing3', 'testing4'))), by=a]
#   a b                                   c
#1: 1 5 testing1,testing2,testing3,testing4
#2: 2 6 testing1,testing2,testing3,testing4
#3: 3 7 testing1,testing2,testing3,testing4
#4: 4 8 testing1,testing2,testing3,testing4

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

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