![](/img/trans.png)
[英]Issue when joining data.tables with greater than and lesser than signs in column names
[英]How do I return data.tables that contain lists or vectors of length greater than 1 in a cell?
我想在j
中指定要在給定列中返回完整列表。 在下面,當我嘗試返回c
列中的列表時,它會對其進行擴展,以便c
中的每個單元格都是單個項目。 我實際上希望返回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
您說您想要這個:
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"
但是我懷疑你想要這個:
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.