如何用前一列的内容替换包含 NA 的列?

How to replace columns containing NA with the contents of the previous column?

我有一个大型数据框,其中包含包含 NA 值的随机列。它看起来像这样:

     2002-06-26 2002-06-27   2002-06-28   2002-07-01 2002-07-02   2002-07-03   2002-07-05
1  US1718711062         NA BMG4388N1065 US0116591092         NA AN8068571086 GB00BYMT0J19
2  US9837721045         NA US0025671050 US03662Q1058         NA BMG3223R1088 US0097281069
3                       NA US00847J1051 US06652V2088         NA BMG4388N1065 US0305061097
4                       NA US04351G1013 US1046741062         NA BMG7496G1033 US03836W1036
5                       NA US2925621052 US1431301027         NA CA88157K1012 US06652V2088
6                       NA US34988V1061 US1897541041         NA CH0044328745 US1547604090
7                       NA US3596941068 US2053631048         NA GB00B5BT0K07 US1778351056
8                       NA US4180561072 US2567461080         NA IE00B5LRLL25 US1999081045
9                       NA US4198791018 US2925621052         NA IE00B8KQN827 US3498531017
10                      NA US45071R1095 US3989051095         NA IE00BGH1M568 US42222N1037

我需要一个代码来标识 NA 列并使用上一列的内容填充它们。因此,例如列“2002-06-27”应该包含 "US1718711062" 和 "US9837721045"。 NA 列的间隔不规则。

列也是随机长度的,有些只包含一个元素,所以我认为识别没有值的列的最佳方法是像这样查看第一行:

row.has.na <- which(is.na(data[1,]))

[1] 2 5

这应该有效。请注意,如果两个(或更多)NA 列彼此相邻,这也适用。也许有办法绕过 while-loop,但是...

# Create some data
data <- data.frame(col1 = 1:10, col2 = NA, col3 = 10:1, col4 = NA, col5 = NA, col6 = NA)

# Find which columns contain NA in the first row
col_NA <- which(is.na(data[1,]))

# Select the previous columns
col_replace <- col_NA - 1

# Check if any NA columns are next to each other and fix it: 
while(any(diff(col_replace) == 1)){
    ind <- which(diff(col_replace) == 1) + 1
    col_replace[ind] <- col_replace[ind] - 1
}

# Replace the NA columns with the previous columns
data[,col_NA] <- data[,col_replace]

   col1 col2 col3 col4 col5 col6
1     1    1   10   10   10   10
2     2    2    9    9    9    9
3     3    3    8    8    8    8
4     4    4    7    7    7    7
5     5    5    6    6    6    6
6     6    6    5    5    5    5
7     7    7    4    4    4    4
8     8    8    3    3    3    3
9     9    9    2    2    2    2
10   10   10    1    1    1    1

为了完成我的评论:因为您已经计算了 row.has.naNA 列的索引向量,这里有一种使用它并获得您需要的方法:

data[, row.has.na] <- data[, row.has.na - 1]