我有以下数据表:
>
dt
folder type value
1: 1 prob 0.683541877259838
2: 1 label 1
3: 1 prob 0.495066106474617
4: 1 label 1
5: 1 prob 0.640406598486647
6: 1 label 1
7: 1 prob 0.482671032167111
8: 1 label 1
9: 1 prob 0.683541877259838
10: 1 label 1
11: 1 prob 0.640406598486647
12: 1 label 0
13: 1 prob 0.726221221435974
14: 1 label 0
15: 1 prob 0.565276604111103
16: 1 label 1
17: 1 prob 0
18: 1 label 0
19: 1 prob 0.565276604111103
20: 1 label 1
21: 1 prob 0.726221221435974
22: 1 label 1
23: 1 prob 0.738455509755984
24: 1 label 1
25: 1 prob 1
26: 1 label 0
27: 1 prob 0.640406598486647
28: 1 label 0
29: 1 prob 0.743529095250444
30: 1 label 0
31: 1 prob 0.640406598486647
32: 1 label 0
33: 1 prob 0.726221221435974
34: 1 label 1
35: 1 prob 0.683541877259838
36: 1 label 1
folder type value
我想实现的是一个嵌套的列表列表,如下所示(第一级是“类型”数据表列中包含的变量,第二级是“文件夹”,然后是所有值):
labels[[1]]
[1] 1 1 1 1 1 0
[6] 0 0 1
labels[[2]]
[1] 1 1 1 1 1 0
[6] 0 0 1
prob [[1]]
[1] 0.1 0.21 0.1 0.1 0.43 0.0
[6] 0.0 0 1
prob[[2]]
[1] 0.56 0.64 0.3 0.21 0.81 0.5
[6] 0.33 0.4
我尝试过类似的事情:
dt2 = dt[, {
l1 = list()
l1[[type]][[folder]]= as.list(value)
list(l1)
}, by = list(type,folder)]
dt3 = dt2[, list({
l2 = list()
l2[[type]][[folder]] = unlist(V1, recursive = FALSE)
l2
}), by = list(type, folder) ]
out = dt3[['V1']]
names(out) = dt3[['type']]
但是结果不是预期的
$prob
$prob$`1`
$prob$`1`$`1`
$prob$`1`$`1`[[1]]
[1] "0.683541877259838"
$prob$`1`$`1`[[2]]
[1] "0.495066106474617"
$prob$`1`$`1`[[3]]
[1] "0.640406598486647"
$prob$`1`$`1`[[4]]
[1] "0.482671032167111"
$prob$`1`$`1`[[5]]
[1] "0.683541877259838"
$prob$`1`$`1`[[6]]
[1] "0.640406598486647"
$prob$`1`$`1`[[7]]
[1] "0.726221221435974"
$prob$`1`$`1`[[8]]
[1] "0.565276604111103"
$prob$`1`$`1`[[9]]
[1] "0"
$prob$`1`$`1`[[10]]
[1] "0.565276604111103"
$prob$`1`$`1`[[11]]
[1] "0.726221221435974"
$prob$`1`$`1`[[12]]
[1] "0.738455509755984"
$prob$`1`$`1`[[13]]
[1] "1"
$prob$`1`$`1`[[14]]
[1] "0.640406598486647"
$prob$`1`$`1`[[15]]
[1] "0.743529095250444"
$prob$`1`$`1`[[16]]
[1] "0.640406598486647"
$prob$`1`$`1`[[17]]
[1] "0.726221221435974"
$prob$`1`$`1`[[18]]
[1] "0.683541877259838"
$label
$label$`1`
$label$`1`$`1`
$label$`1`$`1`[[1]]
[1] "1"
$label$`1`$`1`[[2]]
[1] "1"
$label$`1`$`1`[[3]]
[1] "1"
$label$`1`$`1`[[4]]
[1] "1"
$label$`1`$`1`[[5]]
[1] "1"
$label$`1`$`1`[[6]]
[1] "0"
$label$`1`$`1`[[7]]
[1] "0"
$label$`1`$`1`[[8]]
[1] "1"
$label$`1`$`1`[[9]]
[1] "0"
$label$`1`$`1`[[10]]
[1] "1"
$label$`1`$`1`[[11]]
[1] "1"
$label$`1`$`1`[[12]]
[1] "1"
$label$`1`$`1`[[13]]
[1] "0"
$label$`1`$`1`[[14]]
[1] "0"
$label$`1`$`1`[[15]]
[1] "0"
$label$`1`$`1`[[16]]
[1] "0"
$label$`1`$`1`[[17]]
[1] "1"
$label$`1`$`1`[[18]]
[1] "1"
关于如何实现此列表格式的任何建议?
答案 0 :(得分:0)
解决了以下问题:
> #create support lists
> f.list = vector('list', 2) #list for variables
> o.list=vector('list',n_folds) #list for folds
>
> #list the levels of type (variables)
> t<-as.data.frame(table(dt$type))
>
> for (i in 1:2) {
+ for (j in 1:n_folds) {
+ o.list[[j]] = as.numeric(filter(dt, folder==j & type==t[i,1])$value)
+ }
+ f.list[[i]] = o.list
+ }
>
> #rename list names
> names(f.list)[[1]]="label"
> names(f.list)[[2]]="prob"
>
> str(f.list)
List of 2
$ label:List of 10
..$ : num [1:18] 0 1 1 0 1 0 0 1 1 0 ...
..$ : num [1:19] 0 1 1 1 1 1 0 0 1 1 ...
..$ : num [1:18] 1 1 1 0 1 1 1 0 1 1 ...
..$ : num [1:19] 1 1 1 1 1 1 1 1 0 0 ...
..$ : num [1:20] 0 1 1 1 0 1 0 0 1 1 ...
..$ : num [1:19] 1 1 1 1 1 1 0 0 1 0 ...
..$ : num [1:19] 1 1 1 0 1 0 1 0 1 1 ...
..$ : num [1:18] 1 1 0 0 1 0 0 0 1 1 ...
..$ : num [1:19] 1 1 1 0 0 1 1 1 1 0 ...
..$ : num [1:18] 0 1 1 1 0 0 1 0 1 1 ...
$ prob :List of 10
..$ : num [1:18] 0 0.906 0.691 0.731 0.691 ...
..$ : num [1:19] 0.711 0.673 0.673 0.711 0.673 ...
..$ : num [1:18] 1 0.619 0.668 0.668 0.788 ...
..$ : num [1:19] 0.688 0.723 0.723 0.723 0.688 ...
..$ : num [1:20] 0.696 0.696 0.696 0.696 0.696 ...
..$ : num [1:19] 0.645 0.696 0.696 0.645 0.696 ...
..$ : num [1:19] 0.645 0.691 0.645 0.645 0.691 ...
..$ : num [1:18] 0.66 0.66 0.59 0.617 0.704 ...
..$ : num [1:19] 0.836 0.73 0.689 0.689 0.836 ...
..$ : num [1:18] 0.688 0.651 0.804 0.688 0.579 ...
> f.list
$label
$label[[1]]
[1] 0 1 1 0 1 0 0 1 1 0 1 1 1 1 1 0 0 1
$label[[2]]
[1] 0 1 1 1 1 1 0 0 1 1 0 0 0 1 1 0 1 1 0