Spliiting a time series and applying different functions to different columns in R -


मान लीजिए कि यह डेटाफ्रेम (डीटी) है:

  datetime price1 price2 2011-01- 04 22:00:20 1 7 2011-01-04 22:01:37 2 8 2011-01-04 22:01:57 3 9 2011-01-04 22:03:03 4 10 2011-01-04 22 : 03: 32 5 11 2011-01-04 22:03:45 6 12   

मैं डेटा समय (1 मिनट अंतराल) द्वारा डेटा विभाजित करना चाहता हूं और ढूंढता हूं: < पी> कीमत 1 के लिए: पिछली बार अवलोकन शून्य से पहले।

मूल्य 2 के लिए: सभी मूल्यों का योग।

  समय अंतर राशि 2011-01-04 22:00:00 1-1 = 0 7 2011-01-04 22:01:00 3-2 = 1 17 2011-01-04 22:03:00 6-4 = 2 33 डीटी $ समय & lt; - कट (dt $ datetime, "1 मिनट") dt1min & lt; -ddply (डीटी,। (समय), फ़ंक्शन (x))   

मुझे फ़ंक्शन (x) को कैसे परिभाषित करना चाहिए? धन्यवाद।

यह मेरा समाधान data.table पैकेज का उपयोग कर रहा है: < / p>

  लाइब्रेरी (डेटा योग्य) डीटी & lt; - data.table (datetime = c ("2011-01-04 22:00:20", "2011-01-04 22:01: 37 "," 2011-01-04 22:01:57 "," 2011-01-04 22:03:03 "," 2011-01-04 22:03:32 "," 2011-01-04 22: 03:45 "), प्राइस 1 = सी (1,2,3,4,5,6), प्राइस 2 = सी (7, 8, 9, 10, 11, 12)) [dt [, datetime: = as.POSIXct () दिनांक समय)] # टाइमस्टैम्प डीटी [, समय: = प्रारूप (दिनांक समय, "% Y-% m-% d% H:% M")] # # समय का स्तंभ बनाओ, अब विभाजन लागू करें, गठबंधन डीटी [, सूची ( <= code>   

आउटपुट है :

  समय अंतर राशि 1: 2011-01-04 22:00 0 7 2: 2011-01-04 22:01 1 17 3: 2011-01-04 22:03 2 33    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -