My task is to assign column names and group id to data inside an environment.
I have two cases below. The first one has data created using data.frame()
and in case-2, the data is created using data.table()
.
The first case is showing error, but the second one works perfectly well. Why is the error occurring in case-1, but not in case-2? Is there a better way to use set functions of data.table
inside an environment?
library('data.table')
Case 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:
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.
Case 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
}
Session Information
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
The correct answer is Table. 'Table' function is used to create a data table in MS Excel. The data tables allow the user to see the results of a variety of different inputs all at once.
On the Data tab, in the Data Tools group or Forecast group (in Excel 2016), click What-If Analysis > Data Table (in the Data Tools group or Forecast group of Excel 2016). In the Row input cell field, enter the reference to the input cell for the input values in the row. Type cell B4 in the Row input cell box.
Data tables contain data in a tabular form (it is the equivalent of two-dimensional arrays in programming). A data table contains rows and columns and each item stored in the data table can be retrieved through its unique row and column number.
data. table(DT) is TRUE. To better description, I put parts of my original code here. So you may understand where goes wrong.
Use in DataTables. Where a parameter is shown as accepting a function type, or a method returning a function type, it indicates that a function can be passed in (be it as a function assigned to a variable, or an anonymous function) / returned. Functions in DataTables are frequently used for callbacks.
A Data Table will show you how by changing the certain values in your function, you can change the result of the formula. It stores the results of many variable scenarios in one table so that you can finalize the best scenario for your business or project.
Thank you. A set function retrieves a set from a dimension, hierarchy, level, or by traversing the absolute and relative locations of members within these objects, constructing sets in a variety of ways. Set functions, like member functions and tuple functions, are essential to negotiating the multidimensional structures found in Analysis Services.
A function is a method that we can use to make a data operation, such as adding all the values together or finding the average. Fortunately, SQL already has a set number of these functions defined! Table showing SQL Set Function Syntax and Description.
Well, I just changed the way to create the group column and it worked fine to me. I also used names()
instead of ls()
:
# set column names and value as group id
for(i in names(my_env)){
with(my_env, setnames( x = my_env[[i]], c('x', 'y'))) # assign column name by reference
my_env[[i]][,"group"] <- "0_0" # assign group column with a value
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With