2015-11-22 9 views
5

मेरी सरल data.table से, उदाहरण के लिए, इस तरह:"ऑब्जेक्ट 'ansvals' नहीं मिला" त्रुटि - इसका क्या मतलब है?

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 

इस ठीक काम करता है जब मैं स्पष्ट रूप से स्तंभ नाम से संकेत मिलता है ।

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 

मैं इस त्रुटि के बारे में कोई भी जानकारी अब तक नहीं मिल सका: लेकिन जब मैं उन्हें चर में है और with=F इस्तेमाल करने की कोशिश, यह एक त्रुटि देता है।

उत्तर

7

कारण है कि आपको यह त्रुटि संदेश मिल रहा है कि जब with=FALSE का उपयोग कर आप data.table बता j के इलाज के लिए जैसे कि यह एक dataframe थे। इसलिए यह कॉलमनाम के वेक्टर की अपेक्षा करता है और j में new=.N के रूप में मूल्यांकन की अभिव्यक्ति नहीं है।

?data.table के प्रलेखन से with के बारे में: dt1[, (colShow), 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.

जब आप with=FALSE उपयोग करते हैं, आप () इस तरह से पहले एक . बिना j में COLUMNNAMES का चयन करने के लिए है। अन्य विकल्प dt1[, c(colShow), with=FALSE] या dt1[, colShow, with=FALSE] हैं। एक ही परिणाम dt1[, .(col3)]

का उपयोग कर निष्कर्ष निकालने के लिए प्राप्त किया जा सकता: with = FALSE कॉलम data.frame तरह से चयन करने के लिए प्रयोग किया जाता है। तो, आपको ऐसा करना चाहिए।

इसके अलावा by = colBy का उपयोग करके आप data.table कह रहे हैं j जो with = FALSE साथ विरोधाभास में है मूल्यांकन करने के लिए।

?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.table की 1.d and 1.g of the introduction vignette इंगित करता है।


ansvalsdata.table internals में इस्तेमाल एक नाम है। जहां यह ctrl + (Windows) या cmd + (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/कैसे-से-सेट-एकाधिक-कॉलम-और-चयनित-पंक्ति-इन-डेटा-टेबल-टू-वैल्यू-अन्य/33774525 # 33774525)। आप [प्रारंभ करने की मार्गदर्शिका] भी पढ़ना चाहेंगे (https://github.com/Rdatatable/data.table/wiki/Getting-started) – Jaap

+0

अच्छी तरह से, उन उदाहरणों में 'by = 'का उपयोग बिल्कुल नहीं किया जाता है, यह यह काफी अलग बनाता है ... मैं फिर से शुरू करने की मार्गदर्शिका पढ़ूंगा और शायद एक अलग प्रश्न पोस्ट करूँगा जो मुझे बिल्कुल चाहिए। –

1

त्रुटि object 'ansvals' not found मुझे एक बग की तरह दिखता है। यह या तो एक सहायक संदेश होना चाहिए या सिर्फ काम करना चाहिए। मैंने इस प्रश्न पर वापस लिंक करने के लिए issue #1440 दायर किया है, धन्यवाद।

जाप पूरी तरह से सही है।उसके जवाब से पर के बाद, आप j इस तरह में get() उपयोग कर सकते हैं:

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() 'एक अच्छा समाधान हो सकता है :) –

संबंधित मुद्दे