1

I have the following data.frame

> head(k)
              begin               end            type total_replies  rpm
1 03/22/15 00:00:00 03/22/15 00:05:00   178.19                 0.50 0.10
2              <NA>              <NA>   178.19.6               0.50 0.10
3 03/22/15 00:05:00 03/22/15 00:10:00   178.1963               0.50 0.10
4              <NA>              <NA>   178.19.75              0.50 0.10
5              <NA>              <NA>   12.221.253             0.75 0.15
6 03/22/15 00:10:00 03/22/15 00:15:00   101.254                1.50 0.30

The NA values correspond to the timestamp above them respectively, meaning the timestamps in row 2 should be the same as row 1 and so on

How do I fill the NA values to match the timestamp above them ?

Frank
  • 66,179
  • 8
  • 96
  • 180
user3006691
  • 435
  • 3
  • 7
  • 16

2 Answers2

1

I think something like this should work

for (i in 1:nrow(k)) {if is.na(k$begin[i]) {
k$begin[i] = k$begin[i-1]}}
ytk
  • 2,787
  • 4
  • 27
  • 42
0

Basically, you have to do the following:

replace <- function(dataFrame, forField){
    for (i in c(1:nrow(dataFrame))){
        if (is.na(dataFrame[i,forField])){
            dataFrame[i,forField] <- dataFrame[i-1,forField]
        }
    }
    return (dataFrame)
}

and call the function by

replace(yourDataFrame, "begin") 

to replace all begin fields. The same goes for end.

Update output:

enter image description here