Domanda

    summary(DF)

>fx_code          date               fx_spot              fx_fwd         implied_fx_vol  
 AUD    : 171   Min.   :2000-01-31   Min.   :    0.394   Min.   :-320.000   Min.   : 1.000  
 BRL    : 171   1st Qu.:2003-07-31   1st Qu.:    1.623   1st Qu.:  -2.615   1st Qu.: 7.180  
 CAD    : 171   Median :2007-02-28   Median :    6.117   Median :   6.070   Median : 9.842  
 CHF    : 171   Mean   :2007-02-28   Mean   :  449.477   Mean   :  63.569   Mean   :10.656  
 CLP    : 171   3rd Qu.:2010-09-30   3rd Qu.:   43.475   3rd Qu.:  64.055   3rd Qu.:12.809  
 COP    : 171   Max.   :2014-03-31   Max.   :12360.000   Max.   :1438.800   Max.   :62.810  
 (Other):4275                                            NA's   :310        NA's   :783     

then I fill in missing values with values from the previous period

    DF2 <- ddply(DF, .(fx_code), na.locf)
    summary(DF2)

>   fx_code              date             fx_spot             fx_fwd          implied_fx_vol    
 Length:5301        Length:5301        Length:5301        Length:5301        Length:5301       
 Class :character   Class :character   Class :character   Class :character   Class :character  
 Mode  :character   Mode  :character   Mode  :character   Mode  :character   Mode  :character     

this converts everything to character format. any ideas on how to fix this?> thanks in advance

È stato utile?

Soluzione

1) na.locf works on zoo objects, vectors and matrices, not data.frames, so try this which applies it separately to each column and then reconstructs the data.frame:

ddply(DF, .(fx_code), function(x) replace(x, TRUE, lapply(x, na.locf))

2) or this:

na.locf.data.frame <- 
  function(object, ...) replace(object, TRUE, lapply(object, na.locf, ...))
ddply(DF, .(a), na.locf)

3) If the dates of DF are unique within fx_code then it can be represented as a wide zoo object but not as a long one as zoo objects are based on matrices or vectors so their columns must all be of the same class. For the data frame shown this would work if the dates are unique within fx_code:

z <- read.zoo(DF, split = 1, index = 2)
na.locf(z)

For example, try it with this:

 DF <- data.frame(a = c("a", "a", "b", "b"), b = Sys.Date() + 0:3, c = 1:4)

Note

Since this was written na.locf now works on data frames as well.

Altri suggerimenti

You can also use colwise(na.locf)(df) to avoid the class conversion.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top