"Ansava object not found" error - what does it mean?

from my simple data table, for example, for example:

dt1 <- fread(" col1 col2 col3 AAA ab cd BBB ef gh BBB ij kl CCC mn nm") 

I am making a new table, for example, as follows:

 dt1[, .(col3, new=.N), by=col1] > col1 col3 new >1: AAA cd 1 >2: BBB gh 2 >3: BBB kl 2 >4: CCC op 1 

this works fine when I specify column names explicitly. But when I try them in variables and try to use with=F , this gives an error:

 colBy <- 'col1' colShow <- 'col3' dt1[, .(colShow, 'new'=.N), by=colBy, with=F] # Error in `[.data.table`(dt1, , .(colShow, new = .N), by = colBy, with = F) : object 'ansvals' not found 

I have not yet been able to find information about this error.

+5
source share
2 answers

The reason you get this error message is because when using with=FALSE you pass data.table to handle j , as if it were a data file. Therefore, it expects a column vector, not an expression, which should be evaluated in j as new=.N .

From the documentation ?data.table about with :

By default, with=TRUE and j is evaluated inside the x frame; column names can be used as variables. When with=FALSE j is a column vector symbol or a numeric column vector at select, and the return value is always data.table.

When you use with=FALSE , you need to select the column names in j without . before () as follows: dt1[, (colShow), with=FALSE] . Other options: dt1[, c(colShow), with=FALSE] or dt1[, colShow, with=FALSE] . The same result can be obtained using dt1[, .(col3)]

To summarize: with = FALSE used to select columns using the data.frame method. So you have to do it as such.

Also, using by = colBy , you tell data.table to evaluate j , which contradicts with = FALSE .

From the documentation ?data.table about j :

One column name, one instance of column columns, list() of column name expressions, an expression or function call that evaluates a list (including data.frame and data.table, which are lists) or (when with=FALSE ) a vector of names or positions for Choose.

j is evaluated inside the data.table frame; that is, it sees the names of the columns as if they were variables. Use j=list(...) to return multiple columns and / or column expressions. One column or one expression returns this type, usually a vector. See examples.

See also paragraphs 1.d and 1.g of the data table introduction vignette .


ansvals is the name that is used in data.table. You can see where it appears in the code using ctrl + f (Windows) or cmd + f (macOS) here .

+7
source

The error object 'ansvals' not found looks like an error to me. This should be a helpful message or just work. I filed question No. 1440 , citing this question, thanks.

Jaap is completely right. Following his answer, you can use get() in j as follows:

 dt1 # col1 col2 col3 #1: AAA ab cd #2: BBB ef gh #3: BBB ij kl #4: CCC mn nm colBy #[1] "col1" colShow #[1] "col3" dt1[,.(get(colShow),.N),by=colBy] # col1 V1 N #1: AAA cd 1 #2: BBB gh 2 #3: BBB kl 2 #4: CCC nm 1 
+1
source

Source: https://habr.com/ru/post/1236534/


All Articles