अनियमित डेटा को नियमित बनाना

R में वित्तीय डेटा का इम्पोर्ट और प्रबंधन

Joshua Ulrich

Quantitative Analyst & quantmod Co-Author and Maintainer

नियमित date-time सीक्वेंस

  • समय ऑब्ज़र्वेशन समान अंतराल पर हों
  • seq() मेथड से नियमित date-time सीक्वेंस बनाएँ:
    • seq.Date()
    • seq.POSIXt() (POSIXct और POSIXlt)
from_date <- as.Date("2017-01-01")
to_date <- as.Date("2017-01-03")
date_seq <- seq(from = from_date, to = to_date, by = "day")
R में वित्तीय डेटा का इम्पोर्ट और प्रबंधन
  • start() पहला इंडेक्स मान
  • end() आखिरी इंडेक्स मान
regular_xts <- xts(seq_along(date_seq), order.by = date_seq)
start(regular_xts)
"2017-01-01"
end(regular_xts)
"2017-01-03"
seq(from = start(regular_xts), to = end(regular_xts), by = "day")
"2017-01-01" "2017-01-02" "2017-01-03"
R में वित्तीय डेटा का इम्पोर्ट और प्रबंधन

Zero-width xts ऑब्जेक्ट्स

  • इंडेक्स वाला xts ऑब्जेक्ट, डेटा नहीं
zero_width_xts <- xts(, order.by = date_seq)
zero_width_xts
Data:
numeric(0)
Index:
 Date[1:3], format: "2017-01-01" "2017-01-02" "2017-01-03"
str(zero_width_xts)
An 'xts' object of zero-width
R में वित्तीय डेटा का इम्पोर्ट और प्रबंधन

अनियमित डेटा से नियमित बनाना

  • नियमित सीक्वेंस की हर date-time पर ऑब्ज़र्वेशन जोड़ें
  • परिणाम में NA आएगा
R में वित्तीय डेटा का इम्पोर्ट और प्रबंधन

अनियमित xts को नियमित zero-width xts से मर्ज करें

irregular
           Price
2017-01-02 20.01
2017-01-04 20.02
2017-01-10 20.05
date_seq <- seq(from = start(irregular),
                to = end(irregular),
                by = "day")
regular_xts <- xts(, date_seq)
R में वित्तीय डेटा का इम्पोर्ट और प्रबंधन

अनियमित xts को नियमित zero-width xts से मर्ज करें

merge(irregular, regular_xts)
           Price
2017-01-02 20.01
2017-01-03    NA
2017-01-04 20.02
2017-01-05    NA
2017-01-06    NA
2017-01-07    NA
2017-01-08    NA
2017-01-09    NA
2017-01-10 20.05
R में वित्तीय डेटा का इम्पोर्ट और प्रबंधन

मिसिंग वैल्यू भरना

merged_xts <- merge(irregular, regular_xts)

na.locf(merged_xts)
           Price
2017-01-02 20.01
2017-01-03 20.01
2017-01-04 20.02
2017-01-05 20.02
2017-01-06 20.02
2017-01-07 20.02
2017-01-08 20.02
2017-01-09 20.02
2017-01-10 20.05
R में वित्तीय डेटा का इम्पोर्ट और प्रबंधन

मिसिंग वैल्यू भरना

merge(irregular, regular_xts, fill = na.locf)
           Price
2017-01-02 20.01
2017-01-03 20.01
2017-01-04 20.02
2017-01-05 20.02
2017-01-06 20.02
2017-01-07 20.02
2017-01-08 20.02
2017-01-09 20.02
2017-01-10 20.05
R में वित्तीय डेटा का इम्पोर्ट और प्रबंधन

अभ्यास करते हैं!

R में वित्तीय डेटा का इम्पोर्ट और प्रबंधन

Preparing Video For Download...