ploop结果返回多个表,如何将多个表按行进行拼接?

有如下代码:

def getTable(n){
	t = table(1..2500 as id)
	t["org"] = rand(n,2500)
	t["orgdiv3"] = rand(n\3,2500)
	t["orgdiv6"] = rand(n\6,2500)
	t["orgdiv9"] = rand(n\9,2500)		
	t.rename!(t.colNames()+string(n))
	return t
}

result = ploop(getTable,0+(1..1000)*10 )
finalResult = result[0]
for(i in 1..(size(result)-1)){
	finalResult = finalResult.join(result[i])
}

我用for循环将ploop的结果行拼接成一张表,这样的速度很慢,要花费10s左右,要怎么做能降低耗时?


请先 登录 后评论

1 个回答

xuehaiwuya

可以把返回值转换成矩阵,利用concatMatrix()函数提高合并效率,代码如下所示:

def getTable(n){
	t = table(1..2500 as id)
	t["org"] = rand(n,2500)
	t["orgdiv3"] = rand(n\3,2500)
	t["orgdiv6"] = rand(n\6,2500)
	t["orgdiv9"] = rand(n\9,2500)		
	t.rename!(t.colNames()+string(n))
	matriX = matrix(t)
	colName = t.colNames()
	return matriX,colName
}

timer{
	result = ploop(getTable,0+(1..1000)*10 )
	value,colName = result[,0],(result[,1]).flatten()
	output =concatMatrix(value).rename!(colName)
	tmp = table(output)
}

耗时为300ms左右,比for循环块了30倍左右

请先 登录 后评论
  • 1 关注
  • 0 收藏,704 浏览
  • wangsenxiao 提出于 2023-03-21 15:12

相似问题