[datatable-help] command := works with toy data but causes error with real data

carlsutton suttoncarl at ymail.com
Thu Dec 22 01:58:54 CET 2016


Hi

Thanks to the help from this list I was able to get the toy data to work and
subsequently got cast to work.   With anticipation of wonderful things to
happen with real data, I copied the command from toy data to melt real data
and R complained.

What I would like to know:
1.  What is wrong with the code and
2.  What causes this error message?  I have read the help page, searched the
web, and am just as clueless now and when I started trying to find out what
stupid thing I did to cause this error.

Here is the code and error message.
 class(with(data_1,"var1","var2","var3","var_4"))
        class(data_1)
        system.time(races_melt <-melt(data_1, id = c("var1","var2", 
                "var3"), measure = "var_4")[,c("varType",
                "Seqnc") := tstrsplit(variable,"_")][variable :=
                NULL])

Error message
class(with(data_1,"var1","var2","var3","var_4"))
[1] "character"
>         class(data_1)
[1] "data.table" "data.frame"
>         system.time(races_melt <-melt(data_1, id = c("var1","var2", 
+                 "var3"), measure = "var_4")[,c("varType",
+                 "Seqnc") := tstrsplit(variable,"_")][variable :=
+                 NULL])
Error in `:=`(variable, NULL) : 
  Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are
defined for use in j, once only and in particular ways. See help(":=").
Timing stopped at: 0.06 0 0.06 

This error message occurs "not infrequently" in my work and sometimes it's
my typo's and sometimes I just don't have a clue.  The data always is a
data.table
Below is the toy data that worked (BIG THANK YOU TO R Flores for that)
library(data.table)
library(tidyr)
#  data table for melt and columns split
dt1 <- data.table(a_1 = 1:10, b_2 = 20:29,folks = c("art","brian","ed",
                "rich","dennis","frank", "derrick","paul","fred","numnuts"),
                  a_2 = 2:11, b_1 = 21:30)
#melt(dt1, id = "folks")  #  so far so good
melted <- melt(dt1, id = "folks")[,c("varType","varIndex") :=
                tstrsplit(variable,"_")][,variable:=NULL]
#  melted has 40 observations from stacking a and b variables
#  which have lengths of 20 each
str(melted)




-----
Carl Sutton
--
View this message in context: http://r.789695.n4.nabble.com/command-works-with-toy-data-but-causes-error-with-real-data-tp4727560.html
Sent from the datatable-help mailing list archive at Nabble.com.


More information about the datatable-help mailing list