动态创建data.frame/data.table/tibble

动态创建data.frame/data.table/tibble,r,data.table,dynamic-programming,tibble,R,Data.table,Dynamic Programming,Tibble,我正在计算K个度量和N个会话。我需要创建一个data.frame或data.table或tibble动态(算法)并累积所有结果-如下所示。在这种情况下,K是4,N是2 dtAllResults <- data.table( `Session 1 Metric 1` = strStats.res[[1]], `Session 1 Metric 2` = strStats.res[[2]], `Session 1 Metric 3` = strStats.res[[3]], `

我正在计算K个度量和N个会话。我需要创建一个data.frame或data.table或tibble动态(算法)并累积所有结果-如下所示。在这种情况下,K是4,N是2

dtAllResults <- data.table(
  `Session 1 Metric 1` = strStats.res[[1]],
  `Session 1 Metric 2` = strStats.res[[2]],
  `Session 1 Metric 3` = strStats.res[[3]],
  `Session 1 Metric 4` = strStats.res[[4]],
  `Session 2 Metric 1` = strStats.res[[5]],
  `Session 2 Metric 2` = strStats.res[[6]],
  `Session 2 Metric 3` = strStats.res[[7]],
  `Session 2 Metric 4` = strStats.res[[8]]
)

我认为至少根据你的MWE,不需要NSE。这里有一个选项:

nm <- do.call(paste, CJ(paste("Session", 1L:N), paste("Metric", 1L:K)))
setnames(as.data.table(strStats.res), nm)[]
数据:

set.seed(1)
strStats.res
nm <- do.call(paste, CJ(paste("Session", 1L:N), paste("Metric", 1L:K)))
setnames(as.data.table(strStats.res), nm)[]
   Session 1 Metric 1 Session 1 Metric 2 Session 1 Metric 3 Session 1 Metric 4 Session 2 Metric 1 Session 2 Metric 2 Session 2 Metric 3 Session 2 Metric 4
1:          0.2655087          0.3721239          0.5728534          0.9082078          0.2016819          0.8983897          0.9446753          0.6607978
set.seed(1)
strStats.res <- list()
N=2
K=4
for (i in seq_len(N*K)) { 
    strStats.res[[i]] <- runif(1) 
}