处理非常大的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 with 8 Gb.

Please advise.


原文:https://stackoverflow.com/questions/50665119
2024-04-23 19:04

满意答案

您可以使用条件聚合简化事情:

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 the CASE expression directly inside the aggregate function, COUNT in this case.

Demo here

相关问答

更多

如何在postgres中进行条件聚合?(How to do conditional aggregation in postgres?)

在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 但是,如果你有很多这样的对,...

条件聚合引用表而不是手动输入(Conditional aggregation referencing table instead of manual entry)

如果你想继续使用条件聚合,你可以像这样动态创建你的查询。 DECLARE @Columns NVARCHAR(MAX), @Sql NVARCHAR(MAX) SELECT @Columns = STUFF(( SELECT ',' + CONCAT(QUOTENAME([Columns]), ' = MAX(CASE WHEN [Columns] = ''', [Columns], ''' THEN Result END)') FROM thisTab...

mongodb聚合中的多个条件和(Multiple conditional sums in mongodb aggregation)

尝试这种方法, 按类型返回请求总数 现在,获取不同状态计数的最简单方法是在状态字段上使用带有$ group的聚合管道 db.stackoverflow.aggregate([{ $group: {_id: "$status", count: {$sum:1}} }]) 我们将得到与此类似的结果 { "_id" : "", "count" : 2 } { "_id" : "arrived", "count" : 3 } { "_id" : "ordered", "count" : 4 } 用于检...

MongoDb聚合中的条件展开?(Conditional unwind in MongoDb's aggregation?)

在调用aggregate之前以编程方式构建聚合管道: var pipeline = []; pipeline.push( { // SELECT $project : { "sex" : 1, "salesIndex":1 } }, { // WHERE $match: {"salesIndex": {$gte: index}} } ); if (filteringByDepartme...

MySQL条件文本聚合(MySQL conditional text aggregation)

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, '/',...

数据表聚合中的条件因子水平选择(Conditional Factor Level Selection in Aggregation of Data Table)

我们可以将'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:...

Elasticsearch中多字段的条件聚合(Conditional aggregation on multi-field in Elasticsearch)

如果您构建索引,如下所示: { "concepts": [ { "type": "location", "text": "Raleigh" }, { "type": "location", "text": "Damascus" } ] } 并将映射中的“concepts”字段定义为嵌套对象,您可以应用以下搜索, 在...

使用JOIN进行条件聚合?(Conditional aggregation with JOIN?)

我不确定这是不是你在寻找什么: 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 ...

如何在Mongodb的聚合中添加条件操作?(How to add conditional operation in Mongodb's Aggregation?)

使用$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)

您可以使用条件聚合简化事情: 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; ...

相关文章

更多

ext grid

1.editor grid中 {header: "", width: 75, d ...

extjs4 editor grid 问题

ext4.0 grid.getView().getCell(record,column).dom.s ...

Ext 禁用grid

我调用了grid.disable()方法 为什么表格还能双击 我想禁用双击事件该怎么解决 想请各位高 ...

Ext grid分页的问题

因为我的数据形式是二维数据,所以我用simpleStore+ArrayReader可以很方便的显示出来 ...

怎样改变 Ext grid 的字体大小

由于Ext 默认的字体太小,请问要怎样改变 Ext grid 的字体大小啊,我找了很久没找到,希望能得 ...

Ext grid 出现checkbox和图标移位的问题(在IE)

Ext grid 出现checkbox和图标移位的问题(在IE),请问大家该怎么解决。 图片如附件: ...

Ext的例子 中有关grid渲染数据的问题

我看的是Ext3.4的 \examples\grid 下的 paging.js 效果 就是将数据 渲染 ...

Extjs 怎样将grid的record转换成Json串,然后post到下个画面?

在Extjs中怎样将grid的一个record的数据转换成Json字符串传递给下一个jsp文件?然后下 ...

jqgrid addRowData在grid里没有其他行时可以正确添加,有行时则不能添加

jqgrid addRowData在grid里没有其他行时可以正确添加,有行时则不能添加,但是确实执行 ...

采用了数据库分页技术的grid. 当点了选择全部之后, 怎么才能不让表格不能翻到下一页?

我有一个grid. 采用的是 数据库分页, 将limit和start设置在sql中. 查询数据的时 ...

最新问答

更多

python的访问器方法有哪些

使用方法: class A(object): def foo(self,x): #类实例方法 print "executing foo(%s,%s)"%(self,x) @classmethod def class_foo(cls,x): #类方法 print "executing class_foo(%s,%s)"%(cls,x) @staticmethod def static_foo(x): #静态方法 print "executing static_foo(%s)"%x调用方法: a =

使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)

我认为你必须将两个TableGetway传递给UserTable构造。 你必须改变Module.php看看: public function getServiceConfig() { return array( 'factories' => array( 'User\Model\UserTable' => function($sm) { $userTableGateway = $sm->get('UserTable

透明度错误IE11(Transparency bug IE11)

这是一个渲染错误,由使用透明度触发,使用bootstrap用于在聚焦控件周围放置蓝色光环的box-shadow属性。 可以通过添加以下类覆盖来解决它。 .form-control:hover { -webkit-box-shadow: 0px 0px 5px 0px rgba(0,0,255,1); -moz-box-shadow: 0px 0px 5px 0px rgba(0,0,255,1); box-shadow: 0px 0px 4px 0px rgba(0,0,255,1)

linux的基本操作命令。。。

ls 显示目录 mkdir 建立目录 cd 进入目录

响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)

将z-index设置为.main-nav这将解决您的重叠问题 .main-nav { position:relative; z-index:9; } set z-index to .main-nav This will fix your overlaping issue .main-nav { position:relative; z-index:9; }

在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)

这是因为模式规范"a"打开一个文件以便追加,文件指针在末尾。 如果您尝试从此处读取,则由于文件指针位于EOF,因此没有数据。 您应该打开"r+"进行阅读和写作。 如果在写入之前读取整个文件,则在写入更多数据时,文件指针将正确定位以追加。 如果这还不够,请探索ftell()和fseek()函数。 That is because the mode spec "a" opens a file for appending, with the file pointer at the end. If you

NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)

支持空中接口的数据速率是一回事。 在消除协议开销,等待eeprom写入以及所有需要时间的其他内容之后,您看到的数据速率是完全不同的故事。 长话短说,从标签读取或进行对等传输时的实际数据速率峰值约为2.5千字节/秒。 取决于具体的标签或对等技术,它可能比这慢很多。 The supported data-rates of the air-interface are one thing. The data-rate that you see after removing protocol overhe

元素上的盒子阴影行为(box-shadow behaviour on elements)

它看起来像只在Windows上的Chrome的错误。 我在Google Canary (Chrome 63)中也进行了测试,问题依然存在,所以有可能它不会很快修复。 这个问题是由overflow: auto引起的overflow: auto ,在你的情况下,它可以很容易地通过删除或设置为可见(默认)来解决。 但是 ,将鼠标悬停在右侧(顶部和底部)时,会出现滚动条。 一个解决方案可以设置overflow: hidden的身体,所以预期的结果是所需的。 我想指出,这不是一个很好的解决方案,但我建议暂

Laravel检查是否存在记录(Laravel Checking If a Record Exists)

这取决于您是否要以后与用户合作,或仅检查是否存在。 如果要使用用户对象(如果存在): $user = User::where('email', '=', Input::get('email'))->first(); if ($user === null) { // user doesn't exist } 如果你只想检查 if (User::where('email', '=', Input::get('email'))->count() > 0) { // user found

设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)

$scope.getData= function () { var reader = new FileReader(); reader.onload = $('input[type=file]')[0].files; var img = new Image(); img.src =(reader.onload[0].result); img.onload = function() { if(this.width > 640