I have data in which NA sometimes occurs
df <- read.table(text = "Czas Energia Energia.2 Water
'2023-11-01 00:00:00' 4.08e13 373649997824 NA
'2023-11-01 00:00:15' 4.08e13 373649997824 1359.001
'2023-11-01 00:00:30' 4.08e13 373649997824 1359.001
'2023-11-01 00:00:45' 4.08e13 373649997824 1359.003
'2023-11-01 00:01:00' 4.08e13 373649997824 1359.004
'2023-11-01 00:01:15' 4.08e13 373649997824 1359.006
'2023-11-01 00:01:30' 4.08e13 373649997824 1359.011
'2023-11-01 00:01:45' 4.08e13 373649997824 1359.011
'2023-11-01 00:02:00' 4.08e13 373649997824 1359.015
'2023-11-01 00:02:15' 4.08e13 373649997824 1359.018
'2023-11-01 00:02:30' 4.08e13 373649997824 1359.020
'2023-11-01 00:02:45' 4.08e13 373649997824 1359.021
'2023-11-01 00:03:00' 4.08e13 373649997824 1359.023
'2023-11-01 00:03:15' 4.08e13 373649997824 1359.029
'2023-11-01 00:03:30' 4.08e13 373649997824 1359.030
'2023-11-01 00:03:45' 4.08e13 373649997824 NA
'2023-11-01 00:04:00' 4.08e13 373649997824 1359.031
'2023-11-01 00:04:15' 4.08e13 373653995520 1359.036
'2023-11-01 00:04:30' 4.08e13 373653995520 1359.038
'2023-11-01 00:04:45' 4.08e13 373653995520 1359.12" ,
header = TRUE
)
I want to replace NA, but only in the first line with the value below (I can do it using fill() .direction = "up"). However, there is an additional condition - the values should appear from line 6 at most. If NA appears in the first 6 lines, NA should remain in the first line.
For example, for the data below, NA in the first line replaces the value 1359.001
df <- read.table(text = "Czas Energia Energia.2 Water
'2023-11-01 00:00:00' 4.08e13 373649997824 NA
'2023-11-01 00:00:15' 4.08e13 373649997824 NA
'2023-11-01 00:00:30' 4.08e13 373649997824 1359.001
'2023-11-01 00:00:45' 4.08e13 373649997824 1359.003
'2023-11-01 00:01:00' 4.08e13 373649997824 1359.004
'2023-11-01 00:01:15' 4.08e13 373649997824 1359.006
'2023-11-01 00:01:30' 4.08e13 373649997824 1359.011
'2023-11-01 00:01:45' 4.08e13 373649997824 1359.011
'2023-11-01 00:02:00' 4.08e13 373649997824 1359.015
'2023-11-01 00:02:15' 4.08e13 373649997824 1359.018
'2023-11-01 00:02:30' 4.08e13 373649997824 1359.020
'2023-11-01 00:02:45' 4.08e13 373649997824 1359.021
'2023-11-01 00:03:00' 4.08e13 373649997824 1359.023
'2023-11-01 00:03:15' 4.08e13 373649997824 1359.029
'2023-11-01 00:03:30' 4.08e13 373649997824 1359.030
'2023-11-01 00:03:45' 4.08e13 373649997824 NA
'2023-11-01 00:04:00' 4.08e13 373649997824 1359.031
'2023-11-01 00:04:15' 4.08e13 373653995520 1359.036
'2023-11-01 00:04:30' 4.08e13 373653995520 1359.038
'2023-11-01 00:04:45' 4.08e13 373653995520 1359.12" ,
header = TRUE
)
For example, for the data below, the NA in the first row remains unchanged
df <- read.table(text = "Czas Energia Energia.2 Water
'2023-11-01 00:00:00' 4.08e13 373649997824 NA
'2023-11-01 00:00:15' 4.08e13 373649997824 NA
'2023-11-01 00:00:30' 4.08e13 373649997824 NA
'2023-11-01 00:00:45' 4.08e13 373649997824 NA
'2023-11-01 00:01:00' 4.08e13 373649997824 NA
'2023-11-01 00:01:15' 4.08e13 373649997824 NA
'2023-11-01 00:01:30' 4.08e13 373649997824 1359.011
'2023-11-01 00:01:45' 4.08e13 373649997824 1359.011
'2023-11-01 00:02:00' 4.08e13 373649997824 1359.015
'2023-11-01 00:02:15' 4.08e13 373649997824 1359.018
'2023-11-01 00:02:30' 4.08e13 373649997824 1359.020
'2023-11-01 00:02:45' 4.08e13 373649997824 1359.021
'2023-11-01 00:03:00' 4.08e13 373649997824 1359.023
'2023-11-01 00:03:15' 4.08e13 373649997824 1359.029
'2023-11-01 00:03:30' 4.08e13 373649997824 1359.030
'2023-11-01 00:03:45' 4.08e13 373649997824 NA
'2023-11-01 00:04:00' 4.08e13 373649997824 1359.031
'2023-11-01 00:04:15' 4.08e13 373653995520 1359.036
'2023-11-01 00:04:30' 4.08e13 373653995520 1359.038
'2023-11-01 00:04:45' 4.08e13 373653995520 1359.12" ,
header = TRUE
)