处理非常大的expand.grid?(Dealing with very large expand.grid?)
我给了31个二元变量,为了简单起见,我们称它们为
x_1, ..., x_31
。每个
X_i = c(0, 1)
。我需要构建一个选项树。 我为此使用了expand.grid,但是我的服务器抛出一个错误:
错误:无法分配大小为16.0 Gb的向量。
我知道我可以
sparsify
这个,但我该如何处理大尺寸?使用
sparsify
会提供相同的错误消息,但使用8 Gb
。请指教。
I am given 31 binary variables, for simplicity let's call them
x_1, ..., x_31
.Each
X_i = c(0, 1)
.I need to build a tree of options. I have used expand.grid for this but my server throws an error:
Error: cannot allocate vector of size 16.0 Gb.
I am aware that I can
sparsify
this but how can I deal with the large size?Using
sparsify
gives the same error message but with8 Gb
.Please advise.
原文:https://stackoverflow.com/questions/50665119
最满意答案
您可以使用条件聚合简化事情:
SELECT ThingName, count(ItemNumber) as TotalItems, count(case when ItemStatus='Red' then ItemNumber end) as RedItems, count(case when ItemStatus='Blue' then ItemNumber end) as BlueItems FROM Things GROUP BY ThingName;
因此,不使用使用
CASE
表达式的子查询来获取Total,Red,Blue项的计数,而是直接在聚合函数内使用CASE
表达式,在这种情况下为COUNT
。You can simplify things using conditional aggregation:
SELECT ThingName, count(ItemNumber) as TotalItems, count(case when ItemStatus='Red' then ItemNumber end) as RedItems, count(case when ItemStatus='Blue' then ItemNumber end) as BlueItems FROM Things GROUP BY ThingName;
Hence, instead of using a subquery that uses a
CASE
expression to get count of Total, Red, Blue items, use theCASE
expression directly inside the aggregate function,COUNT
in this case.
相关问答
更多-
在PostgreSQL 9.4+中: SELECT SUM(items) FILTER (WHERE org_id = 123) * 3 + SUM(items) FILTER (WHERE org_id = 124) * 2 FROM mytable 或者,在早期版本中, SELECT SUM(items * CASE org_id WHEN 123 THEN 3 WHEN 124 THEN 2 END) FROM mytable 但是,如果你有很多这样的对,那么将它们 ...
-
如果你想继续使用条件聚合,你可以像这样动态创建你的查询。 DECLARE @Columns NVARCHAR(MAX), @Sql NVARCHAR(MAX) SELECT @Columns = STUFF(( SELECT ',' + CONCAT(QUOTENAME([Columns]), ' = MAX(CASE WHEN [Columns] = ''', [Columns], ''' THEN Result END)') FROM thisTable ...
-
尝试这种方法, 按类型返回请求总数 现在,获取不同状态计数的最简单方法是在状态字段上使用带有$ group的聚合管道 db.stackoverflow.aggregate([{ $group: {_id: "$status", count: {$sum:1}} }]) 我们将得到与此类似的结果 { "_id" : "", "count" : 2 } { "_id" : "arrived", "count" : 3 } { "_id" : "ordered", "count" : 4 } 用于检索这些记录 ...
-
在调用aggregate之前以编程方式构建聚合管道: var pipeline = []; pipeline.push( { // SELECT $project : { "sex" : 1, "salesIndex":1 } }, { // WHERE $match: {"salesIndex": {$gte: index}} } ); if (filteringByDepartment) { ...
-
MySQL条件文本聚合(MySQL conditional text aggregation)[2022-06-30]
SELECT user_id, program, CASE WHEN COUNT(DISTINCT type) = 1 THEN MAX(type) -- one type, use that type WHEN SUM(more = 'refund') = COUNT(DISTINCT type) THEN 'refund' WHEN SUM(IF(LOCATE('/', type), SUBSTRING_INDEX(type, '/', 1), ... -
我们可以将'V3'转换为具有指定顺序指定levels factor 。 mydt[, V3:= factor(V3, levels=c('Level 1', 'Level 2', 'Level 3', 'Level 0'))][, list(V2= mean(as.numeric(V2)), V3= V3[which.min(V3)]) , V1] # V1 V2 V3 #1: 1 8.666667 Level 1 #2: 2 1 ...
-
如果您构建索引,如下所示: { "concepts": [ { "type": "location", "text": "Raleigh" }, { "type": "location", "text": "Damascus" } ] } 并将映射中的“concepts”字段定义为嵌套对象,您可以应用以下搜索, 在嵌套聚合中 ...
-
我不确定这是不是你在寻找什么: SELECT SUM(p.num_pills/r.pills_per_day) FROM prescriptions p INNER join recommendations r ON p.code = r.bnf_code WHERE p.code Like '0403%' 我假设num_pills是处方药的数量, pills_per_day是推荐的药丸数量。 I am not sure if this is what are you looking for: ...
-
使用$sum和$cond db.transaction.aggregate([ /* Group all documents. Sum all type matching 'buy' and store in a field called buy. Sum all type matching 'sell' and store in a field called sell. */ {$group:{ '_id':null, ...
-
多列条件聚合(Multi-column Conditional Aggregation)[2022-09-25]
您可以使用条件聚合简化事情: SELECT ThingName, count(ItemNumber) as TotalItems, count(case when ItemStatus='Red' then ItemNumber end) as RedItems, count(case when ItemStatus='Blue' then ItemNumber end) as BlueItems FROM Things GROUP BY ThingName; 因此,不 ...