2015-11-22 6 views
5

は、例えば、次のように:"オブジェクト 'ansvals' not found"エラー - それはどういう意味ですか?私の簡単なdata.tableから

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

私はこのように、例えば、新しいテーブルを作っています:

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 

私は明示的に列名を示す場合、これは正常に動作します。私は、変数にそれらを持っているとwith=Fを使用しようとする。しかし、これはエラーを与える:

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 

私はこれまでのところ、このエラーに関する情報を見つけることができませんでした。

答えて

7

このエラーメッセージを取得している理由は、with=FALSEを使用しているときに、それがデータフレームであるかのようjを治療するためのdata.tableを伝えることです。したがって、​​としてjで評価される式ではなく、列名のベクトルが期待されます。 ?data.tableのドキュメントから

についてwithdt1[, (colShow), with=FALSE]:あなたはwith=FALSEを使用する場合

By default with=TRUE and j is evaluated within the frame of x; column names can be used as variables. When with=FALSE j is a character vector of column names or a numeric vector of column positions to select, and the value returned is always a data.table.

、あなたがこのような().なしjでれる列名を選択する必要があります。その他のオプションはdt1[, c(colShow), with=FALSE]またはdt1[, colShow, with=FALSE]です。同じ結果を要約するとdt1[, .(col3)]

を使用することによって得ることができる。with = FALSEは、列にdata.frame方法を選択するために使用されます。だからあなたはそれをそうするべきです。

またby = colByを使用することにより、あなたはwith = FALSEと矛盾しているjを評価するためにdata.tableを語っています。 ?data.tableのドキュメントから

についてj

A single column name, single expresson of column names, list() of expressions of column names, an expression or function call that evaluates to list (including data.frame and data.table which are lists, too), or (when with=FALSE) a vector of names or positions to select.

j is evaluated within the frame of the data.table; i.e., it sees column names as if they are variables. Use j=list(...) to return multiple columns and/or expressions of columns. A single column or single expression returns that type, usually a vector. See the examples.

data.table1.d and 1.g of the introduction vignetteポイントを参照してください。


ansvalsdata.table内部で使用される名前です。それはCTRL +F(Windows)またはCMD + F(MacOSの)hereを使用してコード内で表示される場所を確認できます。

+0

は、説明のためにありがとうございました!実際には、列名が変数に格納されているときに 'by = 'を使用する方法がないことを実際に意味していますか? –

+0

@VasilyA確かに可能ですが、あなたは正しい方法でそれを行う必要があります。 [ここ](http://stackoverflow.com/questions/32940580/convert-some-column-classes-in-data-table/32942319#32942319)または[ここ](http://stackoverflow.com/questions/)を参照してください。 33772830/how-to-set-multiple-columns-and-selected-rows-in-data-table-to-other/33774525#33774525)を参照してください。 [スタートガイド](https://github.com/Rdatatable/data.table/wiki/Getting-started) – Jaap

+0

も参照してください。これらの例では 'by ='はまったく使用されていませんが、これはそれは全く違うものになってしまいます...私はもう一度Getting Startedガイドを読んで、正確に何が必要なのかを指定した別の質問を投稿します。 –

1

エラーobject 'ansvals' not foundは私のバグのようです。役に立つメッセージか、ちょうど仕事でなければなりません。私はissue #1440をこの質問にリンクして提出しました。ありがとうございます。

ジャップは完全に正しいです。彼の答えに続き、あなたはこのようなjget()を使用することができます。

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

マットありがとう! 'get()'が良い解決策になるかどうかを質問する質問を投稿するつもりでした:) –

関連する問題