通过引用在函数中添加大量列

时间:2014-05-05 19:34:08

标签: r data.table

我无法弄清楚在这种情况下data.table会发生什么:

fooFun <- function(tbl, totCols) {
    tbl[, paste0("col", 1:totCols) := 0]
}

从一个空的1-col数据表开始。

> tbl = data.table(initialCol=double())

然后按引用添加99列:

> fooFun(tbl, 99)
> colnames(tbl)
  [1] "initialCol" "col1"       "col2"       "col3"       "col4"       "col5"       "col6"       "col7"       "col8"       "col9"       "col10"      "col11"      "col12"      "col13"     
 [15] "col14"      "col15"      "col16"      "col17"      "col18"      "col19"      "col20"      "col21"      "col22"      "col23"      "col24"      "col25"      "col26"      "col27"     
 [29] "col28"      "col29"      "col30"      "col31"      "col32"      "col33"      "col34"      "col35"      "col36"      "col37"      "col38"      "col39"      "col40"      "col41"     
 [43] "col42"      "col43"      "col44"      "col45"      "col46"      "col47"      "col48"      "col49"      "col50"      "col51"      "col52"      "col53"      "col54"      "col55"     
 [57] "col56"      "col57"      "col58"      "col59"      "col60"      "col61"      "col62"      "col63"      "col64"      "col65"      "col66"      "col67"      "col68"      "col69"     
 [71] "col70"      "col71"      "col72"      "col73"      "col74"      "col75"      "col76"      "col77"      "col78"      "col79"      "col80"      "col81"      "col82"      "col83"     
 [85] "col84"      "col85"      "col86"      "col87"      "col88"      "col89"      "col90"      "col91"      "col92"      "col93"      "col94"      "col95"      "col96"      "col97"     
 [99] "col98"      "col99" 

一切看起来都不错。现在添加第100列:

> fooFun(tbl, 100)
> colnames(tbl)
  [1] "initialCol" "col1"       "col2"       "col3"       "col4"       "col5"       "col6"       "col7"       "col8"       "col9"       "col10"      "col11"      "col12"      "col13"     
 [15] "col14"      "col15"      "col16"      "col17"      "col18"      "col19"      "col20"      "col21"      "col22"      "col23"      "col24"      "col25"      "col26"      "col27"     
 [29] "col28"      "col29"      "col30"      "col31"      "col32"      "col33"      "col34"      "col35"      "col36"      "col37"      "col38"      "col39"      "col40"      "col41"     
 [43] "col42"      "col43"      "col44"      "col45"      "col46"      "col47"      "col48"      "col49"      "col50"      "col51"      "col52"      "col53"      "col54"      "col55"     
 [57] "col56"      "col57"      "col58"      "col59"      "col60"      "col61"      "col62"      "col63"      "col64"      "col65"      "col66"      "col67"      "col68"      "col69"     
 [71] "col70"      "col71"      "col72"      "col73"      "col74"      "col75"      "col76"      "col77"      "col78"      "col79"      "col80"      "col81"      "col82"      "col83"     
 [85] "col84"      "col85"      "col86"      "col87"      "col88"      "col89"      "col90"      "col91"      "col92"      "col93"      "col94"      "col95"      "col96"      "col97"     
 [99] "col98"      "col99"  

什么?不存在......现在在函数调用之外添加一列:

> tbl[, newCol := 5]
> colnames(tbl)
  [1] "initialCol" "col1"       "col2"       "col3"       "col4"       "col5"       "col6"       "col7"       "col8"       "col9"       "col10"      "col11"      "col12"      "col13"     
 [15] "col14"      "col15"      "col16"      "col17"      "col18"      "col19"      "col20"      "col21"      "col22"      "col23"      "col24"      "col25"      "col26"      "col27"     
 [29] "col28"      "col29"      "col30"      "col31"      "col32"      "col33"      "col34"      "col35"      "col36"      "col37"      "col38"      "col39"      "col40"      "col41"     
 [43] "col42"      "col43"      "col44"      "col45"      "col46"      "col47"      "col48"      "col49"      "col50"      "col51"      "col52"      "col53"      "col54"      "col55"     
 [57] "col56"      "col57"      "col58"      "col59"      "col60"      "col61"      "col62"      "col63"      "col64"      "col65"      "col66"      "col67"      "col68"      "col69"     
 [71] "col70"      "col71"      "col72"      "col73"      "col74"      "col75"      "col76"      "col77"      "col78"      "col79"      "col80"      "col81"      "col82"      "col83"     
 [85] "col84"      "col85"      "col86"      "col87"      "col88"      "col89"      "col90"      "col91"      "col92"      "col93"      "col94"      "col95"      "col96"      "col97"     
 [99] "col98"      "col99"      "newCol"  

一切都好。现在添加第100列:

> fooFun(tbl, 100)
> colnames(tbl)
  [1] "initialCol" "col1"       "col2"       "col3"       "col4"       "col5"       "col6"       "col7"       "col8"       "col9"       "col10"      "col11"      "col12"      "col13"     
 [15] "col14"      "col15"      "col16"      "col17"      "col18"      "col19"      "col20"      "col21"      "col22"      "col23"      "col24"      "col25"      "col26"      "col27"     
 [29] "col28"      "col29"      "col30"      "col31"      "col32"      "col33"      "col34"      "col35"      "col36"      "col37"      "col38"      "col39"      "col40"      "col41"     
 [43] "col42"      "col43"      "col44"      "col45"      "col46"      "col47"      "col48"      "col49"      "col50"      "col51"      "col52"      "col53"      "col54"      "col55"     
 [57] "col56"      "col57"      "col58"      "col59"      "col60"      "col61"      "col62"      "col63"      "col64"      "col65"      "col66"      "col67"      "col68"      "col69"     
 [71] "col70"      "col71"      "col72"      "col73"      "col74"      "col75"      "col76"      "col77"      "col78"      "col79"      "col80"      "col81"      "col82"      "col83"     
 [85] "col84"      "col85"      "col86"      "col87"      "col88"      "col89"      "col90"      "col91"      "col92"      "col93"      "col94"      "col95"      "col96"      "col97"     
 [99] "col98"      "col99"      "newCol"     "col100"    

现在就在那里。现在再添加20个:

> fooFun(tbl, 120)
> colnames(tbl)
  [1] "initialCol" "col1"       "col2"       "col3"       "col4"       "col5"       "col6"       "col7"       "col8"       "col9"       "col10"      "col11"      "col12"      "col13"     
 [15] "col14"      "col15"      "col16"      "col17"      "col18"      "col19"      "col20"      "col21"      "col22"      "col23"      "col24"      "col25"      "col26"      "col27"     
 [29] "col28"      "col29"      "col30"      "col31"      "col32"      "col33"      "col34"      "col35"      "col36"      "col37"      "col38"      "col39"      "col40"      "col41"     
 [43] "col42"      "col43"      "col44"      "col45"      "col46"      "col47"      "col48"      "col49"      "col50"      "col51"      "col52"      "col53"      "col54"      "col55"     
 [57] "col56"      "col57"      "col58"      "col59"      "col60"      "col61"      "col62"      "col63"      "col64"      "col65"      "col66"      "col67"      "col68"      "col69"     
 [71] "col70"      "col71"      "col72"      "col73"      "col74"      "col75"      "col76"      "col77"      "col78"      "col79"      "col80"      "col81"      "col82"      "col83"     
 [85] "col84"      "col85"      "col86"      "col87"      "col88"      "col89"      "col90"      "col91"      "col92"      "col93"      "col94"      "col95"      "col96"      "col97"     
 [99] "col98"      "col99"      "newCol"     "col100"     "col101"     "col102"     "col103"     "col104"     "col105"     "col106"     "col107"     "col108"     "col109"     "col110"    
[113] "col111"     "col112"     "col113"     "col114"     "col115"     "col116"     "col117"     "col118"     "col119"     "col120"    

看起来不错。现在添加更多:

> fooFun(tbl, 240)
> colnames(tbl)
  [1] "initialCol" "col1"       "col2"       "col3"       "col4"       "col5"       "col6"       "col7"       "col8"       "col9"       "col10"      "col11"      "col12"      "col13"     
 [15] "col14"      "col15"      "col16"      "col17"      "col18"      "col19"      "col20"      "col21"      "col22"      "col23"      "col24"      "col25"      "col26"      "col27"     
 [29] "col28"      "col29"      "col30"      "col31"      "col32"      "col33"      "col34"      "col35"      "col36"      "col37"      "col38"      "col39"      "col40"      "col41"     
 [43] "col42"      "col43"      "col44"      "col45"      "col46"      "col47"      "col48"      "col49"      "col50"      "col51"      "col52"      "col53"      "col54"      "col55"     
 [57] "col56"      "col57"      "col58"      "col59"      "col60"      "col61"      "col62"      "col63"      "col64"      "col65"      "col66"      "col67"      "col68"      "col69"     
 [71] "col70"      "col71"      "col72"      "col73"      "col74"      "col75"      "col76"      "col77"      "col78"      "col79"      "col80"      "col81"      "col82"      "col83"     
 [85] "col84"      "col85"      "col86"      "col87"      "col88"      "col89"      "col90"      "col91"      "col92"      "col93"      "col94"      "col95"      "col96"      "col97"     
 [99] "col98"      "col99"      "newCol"     "col100"     "col101"     "col102"     "col103"     "col104"     "col105"     "col106"     "col107"     "col108"     "col109"     "col110"    
[113] "col111"     "col112"     "col113"     "col114"     "col115"     "col116"     "col117"     "col118"     "col119"     "col120"     "col121"     "col122"     "col123"     "col124"    
[127] "col125"     "col126"     "col127"     "col128"     "col129"     "col130"     "col131"     "col132"     "col133"     "col134"     "col135"     "col136"     "col137"     "col138"    
[141] "col139"     "col140"     "col141"     "col142"     "col143"     "col144"     "col145"     "col146"     "col147"     "col148"     "col149"     "col150"     "col151"     "col152"    
[155] "col153"     "col154"     "col155"     "col156"     "col157"     "col158"     "col159"     "col160"     "col161"     "col162"     "col163"     "col164"     "col165"     "col166"    
[169] "col167"     "col168"     "col169"     "col170"     "col171"     "col172"     "col173"     "col174"     "col175"     "col176"     "col177"     "col178"     "col179"     "col180"    
[183] "col181"     "col182"     "col183"     "col184"     "col185"     "col186"     "col187"     "col188"     "col189"     "col190"     "col191"     "col192"     "col193"     "col194"    
[197] "col195"     "col196"     "col197"     "col198" 

不好。

发生了什么事?

  

1 个答案:

答案 0 :(得分:2)

@Arun指出此问题已在邮件列表中解决:#5204。遵循该线程中的建议,我增加了创建data.table时分配的默认列指针数:

options(datatable.alloccol = 900)

这样,当在已创建的表上增加超过100的列数时,它不会达到100列的默认预分配。这解决了在达到预分配限制时浅层复制对象的基本问题,以便它在此SO问题中产生测试的预期行为。