在大型数据库中的R中创建列

在大型数据库中的R中创建列,r,out-of-memory,aggregate-functions,calculated-columns,large-data,R,Out Of Memory,Aggregate Functions,Calculated Columns,Large Data,如果这个问题已经得到回答,我很抱歉,但我还没有找到答案。我会发布我所有的想法来解决它。问题是数据库太大,我的电脑无法执行此计算(core i7和8 GB RAM)。我使用的是Microsoft R Open 3.3.2和RStudio 1.0.136 我试图在R中的一个大型数据库上创建一个名为tcm.RData(471MB)的新列。我需要的是一个列,它将Shape_面积除以Shape_面积和COD(我称之为ShapeSum)。我第一次尝试用一个公式来计算,但失败了,我又尝试了两个步骤:1)将Sh

如果这个问题已经得到回答,我很抱歉,但我还没有找到答案。我会发布我所有的想法来解决它。问题是数据库太大,我的电脑无法执行此计算(core i7和8 GB RAM)。我使用的是Microsoft R Open 3.3.2和RStudio 1.0.136

我试图在R中的一个大型数据库上创建一个名为tcm.RData(471MB)的新列。我需要的是一个列,它将Shape_面积除以Shape_面积和COD(我称之为ShapeSum)。我第一次尝试用一个公式来计算,但失败了,我又尝试了两个步骤:1)将Shape_面积除以COD,如果成功,将Shape_面积除以ShapeSum

> str(tcm)
    Classes ‘data.table’ and 'data.frame':  26835293 obs. of  15 variables:
    $ OBJECTID  : int  1 2 3 4 5 6 7 8 9 10 ...
    $ LAT       : num  -15.7 -15.7 -15.7 -15.7 -15.7 ...
    $ LONG      : num  -58.1 -58.1 -58.1 -58.1 -58.1 ...
    $ UF        : chr  "MT" "MT" "MT" "MT" ...
    $ COD       : num  510562 510562 510562 510562 510562 ...
    $ AREA_97   : num  1130 1130 1130 1130 1130 ...
    $ Shape_Area: num  255266.7 14875 25182.2 5503.9 95.5 ...
    $ TYPE      : chr  "2" "2" "2" "2" ...
    $ Nomes     : chr  NA NA NA NA ...
    $ NEAR_DIST : num  376104 371332 371410 371592 371330 ...
    $ tc_2004   : chr  "AREA_URBANA" "DESFLORESTAMENTO_2004" "DESFLORESTAMENTO_2004" "DESFLORESTAMENTO_2004" ...
    $ tc_2008   : chr  "AREA_URBANA" "AREA_NAO_OBSERVADA" "AREA_NAO_OBSERVADA" "AREA_NAO_OBSERVADA" ...
    $ tc_2010   : chr  "AREA_URBANA" "PASTO_LIMPO" "PASTO_LIMPO" "PASTO_LIMPO" ...
    $ tc_2012   : chr  "AREA_URBANA" "PASTO_SUJO" "PASTO_SUJO" "PASTO_SUJO" ...
    $ tc_2014   : chr  "AREA_URBANA" "PASTO_LIMPO" "PASTO_LIMPO" "PASTO_SUJO" ...
    - attr(*, ".internal.selfref")=<externalptr> 

> tcm$ShapeSum <- tcm[, Shape_Area := sum(tcm$Shape_Area), by="COD"]
     Error: cannot allocate vector of size 204.7 Mb
     Error during wrapup: cannot allocate vector of size 542.3 Mb
str(tcm) 类“data.table”和“data.frame”:26835293 obs。在15个变量中: $OBJECTID:Int1 2 3 4 5 6 7 8 9 10。。。 $LAT:num-15.7-15.7-15.7-15.7-15.7-15.7。。。 $LONG:num-58.1-58.1-58.1-58.1-58.1-58.1。。。 $UF:chr“MT”“MT”“MT”。。。 $COD:num 510562 510562 510562 510562 510562 510562。。。 $AREA_97:数字1130 1130 1130 1130 1130。。。 $Shape_面积:255266.7 14875 25182.2 5503.9 95.5。。。 $TYPE:chr“2”“2”“2”。。。 $Nomes:chr-NA-NA-NA。。。 $NEAR_DIST:num 376104 371332 371410 371592 371330。。。 $tc_2004:chr“AREA_URBANA”“desflorestatemento_2004”“desflorestatemento_2004”“desflorestatemento_2004”。。。 $tc_2008:chr“区域城市”区域“区域观测”区域“区域观测”区域“区域观测”。。。 $tc_2010:chr“城市区域”“帕斯托_林波”“帕斯托林波”“帕斯托林波”。。。 $tc_2012:chr“城市地区”“帕斯托_苏乔”“帕斯托_苏乔”“帕斯托苏乔”。。。 $tc_2014:chr“城市区域”“帕斯托_林波”“帕斯托林波”“帕斯托苏乔”。。。 -属性(*,“.internal.selfref”)= >tcm$ShapeSum tcm$ShapeSum tcm$ShapeSum tcm$ShapeSum tcm$ShapeSum
library(data.table)

tcm我们可以使用
data.table
方法来创建列,因为在适当的位置进行赋值(
:=
)更有效

library(data.table)
tcm[, ShapeSum := sum(Shape_Area), by = COD]
或者正如@user20650所建议的那样(根据OP的描述)


因为它是一个data.table,所以最好使用
data.table
方法,即
tcm[,ShapeSum:=sum(Shape\u Area),by=COD]
@user20650是的,谢谢可能是这样的。我没有完全阅读描述,我只想告诉你akrun和@user20650。不幸的是,内存错误仍然存在:'>library(data.table)'>load(“tcm.RData”)'>tcm$shapewight tcm$shapewight gc()'已用(Mb)gc触发器(Mb)最大已用(Mb)Ncells 407961 21.8 750400 40.1 750400 40.1 Vcells 416623404 3178.6 1240807029 9466.7 1060617477 8091.9我不知道您为什么要做
tcm$shapewight谢谢,tcm$shapewight是问题所在。我删除了它,现在它可以工作了。:)
> tcm$ShapeSum <- mutate(tcm, ShapeSum = sum(Shape_Area), by="COD", package = "dplyr")
> tcm$ShapeSum <- tcm[, transform(tcm, ShapeSum = sum(Shape_Area)), by="COD"]

> tcm$ShapeSum <- transform(tcm, aggregate(tcm$AreaShape, by=list(Category=tcm$COD), FUN=sum))
library(data.table)

tcm <- fread("yout_tcm_file.txt")

tcm[, newColumn:=oldColumnPlusOne+1]
library(data.table)
tcm[, ShapeSum := sum(Shape_Area), by = COD]
tcm[, ShapeSum := Shape_Area/sum(Shape_Area), by = COD]