如何在环境中使用数据表的set函数

时间:2018-04-23 21:02:45

标签: r data.table

我的任务是将列名和组ID分配给环境中的数据。

我有两个案例。第一个使用data.frame()创建数据,而在案例-2中,数据是使用data.table()创建的。

第一种情况是显示错误,但第二种情况非常有效。为什么错误发生在case-1中,而不是在case-2中?有没有更好的方法在环境中使用data.table的集合函数?

library('data.table')

案例1:

my_env <- new.env()
my_env$d1 <- data.frame(a = 1:5, b = 1:5)
my_env$d2 <- data.frame(a = 1:5, b = 1:5)
my_env$d3 <- data.frame(a = 1:5, b = 1:5)

# set column names and value as group id
for(i in ls(my_env)){
  with(my_env, setDT(get(i))) # convert to data table by reference
  with(my_env, setnames( x = get(i), c('x', 'y')))  # assign column name by reference
  with(my_env, set( x = get(i), j = 'group', value = '0_0')) # assign group column with a value
}

错误:

 Error in set(x = get(i), j = "group", value = "0_0") : 
  Internal error, please report (including result of sessionInfo()) to datatable-help: oldtncol (0) < oldncol (2) but tl of
  class is marked.

案例2:

my_env2 <- new.env()
my_env2$d1 <- data.table(a = 1:5, b = 1:5)
my_env2$d2 <- data.table(a = 1:5, b = 1:5)
my_env2$d3 <- data.table(a = 1:5, b = 1:5)

# set column names and value as group id
for(i in ls(my_env2)){
  # with(my_env, setDT(get(i))) # convert to data table by reference
  with(my_env2, setnames( x = get(i), c('x', 'y')))  # assign column name by reference
  with(my_env2, set( x = get(i), j = 'group', value = '0_0')) # assign group column with a value
}

会话信息

sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] data.table_1.9.7

loaded via a namespace (and not attached):
[1] tools_3.3.2

1 个答案:

答案 0 :(得分:0)

好吧,我只是改变了创建组列的方式,它对我来说很好用。我还使用了al_text.collocation_list() 而不是names()

ls()