填写NAs NOCB(下一个观察向后移动)

时间:2013-12-10 11:02:57

标签: r

我的数据是来自KDB数据库查询的返回,并以列表列表的形式出现 如果我str(myData)

   $ INL:List of 9
  ..$ : int [1:1920] 34200000 34215000 34230000 34245000 34260000 34275000 34290000     
  ..$ : Date[1:1920], format: ")*//-06-22" ")*//-06-22" ...
  ..$ : chr [1:1920] "" "" "" "" ...
  ..$ : num [1:1920] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
  ..$ : int [1:1920] NA NA NA NA NA NA NA NA NA NA ...
  ..$ : num [1:1920] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
  ..$ : int [1:1920] NA NA NA NA NA NA NA NA NA NA ...
  ..$ : num [1:1920] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
  ..$ : num [1:1920] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...

at [[1]]  i have a time
at [[2]] i have date 
at [[3]] i have a stock ticker 
at [[4]] i have a bid price
at [[5]] i have bid qty
at [[6]] i have an ask price
at [[7]] i have an ask qty 

我面临的问题是前20个左右的回报是NaN,并且输出中可能还有NaN。

我想用没有NAN的第一行中的结果替换NaN,即如果我们的列表列表看起来像这样

[1]     [2]           [3]    [4]   [5]     [6]    [7]
09:30   Nan           Nan    Nan   Nan     Nan    Nan 
09:31   Nan           Nan    Nan   Nan     Nan    Nan 
09:32   2013:01:01    ABC    100   50      101    35 
09:33   2013:01:01    ABC    100   50      102    60
09:34   Nan           Nan    Nan   Nan     Nan    Nan 
09:35   2013:01:01    ABC    99   40      101    50


This would come out as follows:
[1]     [2]           [3]    [4]   [5]     [6]    [7]
09:30   2013:01:01    ABC    100   50      101    35 
09:31   2013:01:0     ABC    100   50      101    35 
09:32   2013:01:01    ABC    100   50      101    35 
09:33   2013:01:01    ABC    100   50      102    60
09:34   2013:01:01    ABC    99   40       101    50 
09:35   2013:01:01    ABC    99   40       101    50

即,Nan'a被相同coloumn(列表)中的下一个有效条目替换

2 个答案:

答案 0 :(得分:2)

使用class Program { struct StudentID { public int idNumber; public string name; } static void Main(string[] args) { StudentID[] StudentInfo = new StudentID[3]; for (int i = 0; i < StudentInfo.Length; i++) { Console.Write("Enter ID Number: "); StudentInfo[i].idNumber = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter Name: "); StudentInfo[i].name = Convert.ToString(Console.ReadLine()); } int temp = 0; for (int i = 0; i < StudentInfo.Length; i++) { for (int j = 0; j < StudentInfo.Length; j++) { if (StudentInfo[i].idNumber < StudentInfo[j].idNumber) { temp = StudentInfo[i].idNumber; StudentInfo[i].idNumber = StudentInfo[j].idNumber; StudentInfo[j].idNumber = temp; } } } Console.Write("Student ID Sorted with Corresponding Name: "); for ( int i =0; i < StudentInfo.Length; i++) { Console.WriteLine("\nStudent ID : {0} ", StudentInfo[i].idNumber); } Console.ReadLine(); } }

zoo::na.locf

答案 1 :(得分:0)

您可以使用sentinel valuerle

来执行此操作
d <- c(NaN, NaN, 3, 3, 4, NaN, 3,4)
d2 <- ifelse(is.nan(d), -999, d)  # -999 is the sentinel value
r.d2 <- rle(d2)
invalid.posn <- which(r.d2$values == -999)
r.d2$values[ invalid.posn ] <- r.d2$values[ invalid.posn + 1 ]
# If there are NaNs at the end, then the last values will be NA 
inverse.rle(r.d2)

# [1] 3 3 3 3 4 3 3 4