简体   繁体   中英

How do I replace NA values with specific values in an R?

I have a time series dataframe.

But that data contains NA like this:

           KQ11.Open KQ11.High KQ11.Low KQ11.Close KQ11.Volume KQ11.Adjusted
2017-04-05    627.89    630.17   626.94     630.17      923700  630.17
2017-04-06    630.59    630.59   625.20     630.46      873400  630.46
2017-04-07    632.16    633.49   629.61     633.32      833200  633.32
2017-04-10    633.24    633.24   617.16     619.41      865600  619.41
2017-04-11    620.25    625.18   614.21     621.64      813400  622.64
2017-04-12    622.54    622.73   619.27     621.45      695600  621.45
2017-04-13    622.49    624.15   622.38     623.87      742500  623.87
2017-04-14        NA        NA       NA         NA          NA    NA

I want to change Na values to 618.24 618.24 618.24 618.24 742500 618.24

How can I change it?

library(quantmod)
today <- Sys.Date()
kq <- getSymbols("^kq11", from = today-100, to = today, auto.assign = F)
kq

Edited based on the comment by lyngbakr below.

In this specific case I would probably use

df$KQ11.Volume[is.na(df$KQ11.Volume)] = 742500
df[is.na(df)] = 618.24

But that only works in this specific case. OP, is there any specific logic to the numbers you want to use as replacement?

If you only want to change the NAs to your sequence:

df <- read.table(text="date KQ11.Open KQ11.High KQ11.Low KQ11.Close KQ11.Volume KQ11.Adj
2017-04-05    627.89    630.17   626.94     630.17      923700  630.17
2017-04-06    630.59    630.59   625.20     630.46      873400  630.46
2017-04-07    632.16    633.49   629.61     633.32      833200  633.32
2017-04-10    633.24    633.24   617.16     619.41      865600  619.41
2017-04-11    620.25    625.18   614.21     621.64      813400  622.64
2017-04-12    622.54    622.73   619.27     621.45      695600  621.45
2017-04-13    622.49    624.15   622.38     623.87      742500  623.87
2017-04-14        NA        NA       NA         NA          NA    NA",
                 header=TRUE,stringsAsFactors=FALSE)    

df[is.na(df)] <- c(618.24, 618.24, 618.24, 618.24, 742500, 618.24)

        date KQ11.Open KQ11.High KQ11.Low KQ11.Close KQ11.Volume KQ11.Adjusted
1 2017-04-05    627.89    630.17   626.94     630.17      923700        630.17
2 2017-04-06    630.59    630.59   625.20     630.46      873400        630.46
3 2017-04-07    632.16    633.49   629.61     633.32      833200        633.32
4 2017-04-10    633.24    633.24   617.16     619.41      865600        619.41
5 2017-04-11    620.25    625.18   614.21     621.64      813400        622.64
6 2017-04-12    622.54    622.73   619.27     621.45      695600        621.45
7 2017-04-13    622.49    624.15   622.38     623.87      742500        623.87
8 2017-04-14    618.24    618.24   618.24     618.24      742500        618.24

Note: If you don't want to have to hardcode all the NA replacement, you should look at functions na.locf and na.approx from package zoo . It might be handy.

df <- read.table(text="KQ11.Open KQ11.High KQ11.Low KQ11.Close KQ11.Volume KQ11.Adjusted
        2017-04-05    627.89    630.17   626.94     630.17      923700  630.17
        2017-04-06    630.59    630.59   625.20     630.46      873400  630.46
        2017-04-07    632.16    633.49   629.61     633.32      833200  633.32
        2017-04-10    633.24    633.24   617.16     619.41      865600  619.41
        2017-04-11    620.25    625.18   614.21     621.64      813400  622.64
        2017-04-12    622.54    622.73   619.27     621.45      695600  621.45
        2017-04-13    622.49    624.15   622.38     623.87      742500  623.87
        2017-04-14        NA        NA       NA         NA          NA    NA",
             header=TRUE, row.names = 1, stringsAsFactors=FALSE) 

Assuming, you need to replace all the NA values within a column with a constant value of dataframe df as:

df %>% replace_na(list(KQ11.Open =  618.24, KQ11.High = 618.24,
                          KQ11.Low = 618.24, KQ11.Close = 618.24, 
                          KQ11.Volume = 742500, KQ11.Adjusted = 618.24))

Output:

#            KQ11.Open KQ11.High KQ11.Low KQ11.Close KQ11.Volume KQ11.Adjusted
# 2017-04-05    627.89    630.17   626.94     630.17      923700        630.17
# 2017-04-06    630.59    630.59   625.20     630.46      873400        630.46
# 2017-04-07    632.16    633.49   629.61     633.32      833200        633.32
# 2017-04-10    633.24    633.24   617.16     619.41      865600        619.41
# 2017-04-11    620.25    625.18   614.21     621.64      813400        622.64
# 2017-04-12    622.54    622.73   619.27     621.45      695600        621.45
# 2017-04-13    622.49    624.15   622.38     623.87      742500        623.87
# 2017-04-14    618.24    618.24   618.24     618.24      742500        618.24   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM