2011-02-02 80 views
3

遗漏值我有日期和价格清单:填写在表上

Date   Price 
1/3/2000  10.00 
1/5/2000  10.45 
1/7/2000  10.25 
...    ... 

我有日期的所有日期的单独列表:

Date 
1/1/2000 
1/2/2000 
1/3/2000 
... 

我需要他们结合使在缺少价格的日期填写事先价格:

Date   Price 
1/1/2000   10.00 
1/2/2000   10.00 
1/3/2000   10.00 
1/4/2000   10.00 
1/5/2000   10.45 
1/6/2000   10.45 
1/7/2000   10.25 
...    ... 

我目前正试图通过数组列表holdin g数据,但无法正确排列日期,特别是在开始和结束时。我现在正在使用Java/Mysql/JDBC,但也对R开放。感谢您的任何建议。

回答

0

不看代码就很难提供帮助,但好​​像索引不匹配或循环逻辑有问题。

考虑使用使用日期字符串作为键和价格作为值的HashTable或HashMap。

一天中有一天循环查看HashTable中的价格,如果未找到,则使用之前的价格。

0

好吧...我只是在它的拍摄,同时对FON :)

在MySQL中,让我们假设你有两个表,dates_prices和all_dates。然后LEFT加入日期并按日期排序。

如果使用R和MySQL,则可以使用RMySQL包将结果表加载到R. 在R中,您可以使用as.POSIXlt将日期转换为POSIX。你也可能想在R中使用lag函数(但我还不确定,如果这有助于延迟变化的跨度)。

除此之外,如果您想尝试使用“普通”R但希望使用SQL功能,则可以使用R的'qldf`软件包。如果您发布了一些可重现的代码来设置数据..我可以尝试给予更具体的东西。

编辑:

impute包可能是你真正需要的...也here

0

看到这类问题确实需要做的做正确的位。有时使用流程图可以帮助您解决问题。

尝试使用下面的示例代码:

import java.sql.*; 
import java.util.*; 

public class FillDates 
{ 
    public static void fillUnknownDates(Connection c) throws SQLException 
    { 
     // Loads in a Vector of Strings of all the dates 
     Statement state = c.createStatement(); 
     ResultSet results = state.executeQuery("SELECT d FROM Dates ORDER BY d;"); 
     Vector<String> dates = new Vector<String>(); 
     while (results.next()) 
     { 
      dates.add(results.getString("d")); 
     } 

     // Load in a list of all date/price combinations 
     Vector<DatePrice> pairs = new Vector<DatePrice>(); 
     state = c.createStatement(); 
     results = state.executeQuery("SELECT d, p FROM DatePrices ORDER BY d;"); 
     while (results.next()) 
     { 
      pairs.add(new DatePrice(results.getString("d"), results.getString("p"))); 
     } 

     // Now go through the two lists and add missing prices 
     state = c.createStatement(); 
     int dateIndex = 0; 
     DatePrice last = pairs.get(0), current; 
     for (int pairIndex = 1; pairIndex < pairs.size(); pairIndex++) 
     { 
      current = pairs.get(pairIndex); 
      while (dateIndex < dates.size() && dates.get(dateIndex).compareTo(current.getDate()) < 0) 
      { 
       // Batch things up so it takes less time to run 
       state.addBatch("INSERT INTO DatePrices VALUES (\""+dates.get(dateIndex)+"\", \""+current.getPrice+"\");"); 
       dateIndex ++; 
      } 

      last = current; 
     } 
     state.executeBatch(); 
    } 

    // A convenience class 
    public static class DatePrice 
    { 
     private String date, price; 

     public DatePrice(String date, String price) 
     { 
      this.date = date; 
      this.price = price; 
     } 
     public String getDate() 
     { 
      return date; 
     } 
     public String getPrice() 
     { 
      return price; 
     } 
    } 
} 

请注意,这不是完整的,并且你需要在动手之前,改变你的表和字段的名称。

0

这是一个R解决方案。

如果您没有安装这些软件包,请取消注释这两行install.packages行。另外textConnection(Lines1)textConnection(Lines2)只是为了保持示例自包含,并且实际上将被替换为类似"myfile1.dat""myfile2.dat"的数据,假设数据在这些文件中。

它读入数据创建动物园对象z和日期向量dt。然后它将z与其宽度为零的动物园对象(即其日期但没有数据)合并,其日期索引由dt组成。 na.locf(最后一次观察结转)填写,因为fromLast = TRUE

Lines1 <- "Date   Price 
1/3/2000  10.00 
1/5/2000  10.45 
1/7/2000  10.25" 

Lines2 <- "Date 
1/1/2000 
1/2/2000 
1/3/2000" 

# install.packages("zoo") 
# install.packages("chron") 
library(zoo) 
library(chron) 
z <- read.zoo(textConnection(Lines1), header = TRUE, FUN = as.chron) 
dt <- as.chron(scan(textConnection(Lines2), skip = 1, what = "")) 
na.locf(merge(z, zoo(, dt)), fromLast = TRUE) 

以相反的顺序遗漏值的结果是:

> na.locf(merge(z, zoo(, dt)), fromLast = TRUE) 
01/01/00 01/02/00 01/03/00 01/05/00 01/07/00 
    10.00 10.00 10.00 10.45 10.25 

有三个护身符(PDF文件)附带zoo packageR News 4/1服务台文章有关于日期的信息和参考。

1

感谢大家的帮助。以下是我最终做的事情: -I创建了日期匹配的所有索引列表。
- 然后,我将价格插入一个数组中,其数组的元素数与全部时间表相同。 - 然后我创建了3个循环,一个用于第一个匹配时间之前的元素,一个用于最后一个匹配元素之后的元素,最后一个用于之间的所有元素。
- 这三个填补了缺失的价格。

虽然我会分享。感谢你的帮助。

public static void checkLengths(ArrayList<String> masterTimes, ArrayList<String> testTimes, ArrayList<Double> prices){ 
    ArrayList<Double> temp = new ArrayList<Double>(); 
    ArrayList<Integer> matches = new ArrayList<Integer>(); 
    Double[] temp2 = new Double [masterTimes.size()]; 
    int mt = masterTimes.size(); 
    int tt = testTimes.size(); 
     if(mt == tt){ 
      return; 
     }else{ 
      int mast = 0; 
      int test = 0; 
      String mt1 = masterTimes.get(0); 
      String tt1 = testTimes.get(0); 

      test = 0; 
      for(int i = 0; i < masterTimes.size(); i++){ 
       mt1 = masterTimes.get(i); 
       tt1 = testTimes.get(test); 
       System.out.println(" | mt1: " + mt1 + " | tt1: " + tt1); 
        if(mt1.equals(tt1)){ 
         matches.add(i); 
         System.out.println("Inserting: " + i); 
         if(test < testTimes.size()){ 
         test++; 
         } 
         if(test == testTimes.size()){ 
          break; 
         } 
        } 
      } 
      System.out.println("Matches:"); 
      printAL(matches); 

      // puts in known prices. 
      for(int i = 0; i < matches.size(); i++){ 
       int g = matches.get(i); 
       temp2[g] = prices.get(i); 
      } 

      System.out.println("FirstPrices:"); 
      printAR(temp2); 

      // Finds index of first and last matching times. 
      int matcher1 = matches.get(0); 
      int ind = matches.size() - 1; 
      int matcher2 = matches.get(ind); 
      System.out.println("Matcher1:" + matcher1 + " | Matcher2: " + matcher2); 

      // If a price is empty/null, it puts the prior price in it. 
      for(int i = matcher1; i < matcher2; i ++){ 
       System.out.println(i + " | " + temp2[i]); 
       if(temp2[i] == null){ 
        System.out.println(temp2[i] + " | " + temp2[i-1]); 
        temp2[i] = temp2[i-1]; 
       } 
      } 
      System.out.println("SecondPrices:"); 
      printAR(temp2); 

      // Deals with start. 
      for(int i = matcher1; i >= 0; i--){ 
       if(temp2[i] == null){ 
        temp2[i] = temp2[i+1]; 
       } 
      } 

      System.out.println("ThirdPrices:"); 
      printAR(temp2); 

      // Deals with end. 
      for(int i = matcher2; i < temp2.length; i++){ 
       if(temp2[i] == null){ 
        temp2[i] = temp2[i-1]; 
       } 
      } 
      System.out.println("FourthPrices:"); 
      printAR(temp2);    

      prices.clear(); 
      System.out.println("Final Check:"); 

      for (int i = 0; i < masterTimes.size(); i++){ 
       System.out.println(i + " | " + masterTimes.get(i) + " | " + temp2[i]); 
      } 

     } 
}