Матрица для временных рядов с NA

У меня есть фрейм данных, первая строка которого состоит из названий валют, вторая строка состоит из дат, а первая строка состоит из исторических цен на эти даты.

Как я могу преобразовать это в объект временного ряда, столбцы которого состоят из разных валют, а строки - из исторических цен на эти даты? Проблема в том, что у некоторых валют нет цен в определенные дни .... как я могу заполнить их НП и автоматически выровнять их?


person AIM_BLB    schedule 29.08.2018    source источник
comment
Было бы намного проще помочь вам, если бы вы включили образцы данных, ожидаемый результат и код, который вы пробовали.   -  person markus    schedule 29.08.2018


Ответы (1)


Предположим, что фрейм данных называется cryto.markets.post.2108 с именами столбцов «символ», «даты» и «закрытие» для цены закрытия.

Тогда я бы сделал это:

# Creates matrix whose columns are different crypto-currencies and whose rows are different times
#---------------------------------------------------------------------------------------------------#
# N Columns
Col.coins<-unique(crypto.markets.post.2018$symbol);N.coins<-length(Col.coins) # Counts the number of coins in recent history


# N Rows
Row.coins<-seq(from=min(dates),to=max(dates),by=1);N.Rows<-length(Row.coins)

# Initialize Matrix
historical.max.min<-historical.volum<-historical.prices<-matrix(NA,nrow = N.Rows,ncol=N.coins); rownames(historical.prices)<-Row.coins; colnames(historical.prices)<-Col.coins

# Writes data-frame of closing prices
for(j.coin in 1:N.coins){
  time.series.iteration<-crypto.markets.post.2018$symbol == Col.coins[j.coin]#Loads timeseries of this iteration
  time.series.iteration<-crypto.markets.post.2018[time.series.iteration,]
  N.NAs.needed.for.corecsion<-N.Rows-length(as.Date(time.series.iteration$date)) # Determines the difference between the data-size and the matrix's dimensions (for incomplete time-series only :D)
  # Historical Prices
  historical.prices[,j.coin]<-append(rep(NA,N.NAs.needed.for.corecsion),time.series.iteration$close) # Tags on the ts length
  # Historical Volum
  historical.volum[,j.coin]<-append(rep(NA,N.NAs.needed.for.corecsion),time.series.iteration$volume) # Tags on the ts length
  # Difference between max and min
  historical.max.min[,j.coin]<-append(rep(NA,N.NAs.needed.for.corecsion),((time.series.iteration$high)-(time.series.iteration$low))
                                ) # Tags on the ts length
}

person AIM_BLB    schedule 29.08.2018