1

My data looks like this

> sample
  ID  Date
1  1    NA
2  1    NA
3  1 16436
4  1    NA
5  2    NA
6  2 16436
7  2    NA
8  2    NA

The data set I want is

> final
  ID       Date
1  1       <NA>
2  1       <NA>
3  1 2015-01-01
4  1       <NA>
5  2       <NA>
6  2 2015-01-01
7  2 2015-01-01
8  2 2015-01-01

The code I am using is

> final <- sample %>%
+ group_by(ID) %>%
+ mutate(Date = zoo:: na.locf(as.Date(Date,origin= "1970-01-01")))
Error: incompatible size (2), expecting 4 (the group size) or 1

Any help on the issue will be greatly appreciated

zx8754
  • 52,746
  • 12
  • 114
  • 209
Rajarshi Bhadra
  • 1,826
  • 6
  • 25
  • 41
  • 1
    I think there is a typo in your expected output. Shouldn't the 4th element be '2015-01-01`? – akrun Apr 10 '16 at 14:08

1 Answers1

1

We need the na.rm=FALSE. By default, it is na.rm=TRUE which removes the NA values at the beginning so that the output will have less number of elements than in the original dataset. The dplyr::mutate will only work when the output elements have the same length as the original dataset.

library(zoo)
library(dplyr)
sample %>%
     group_by(ID) %>% 
     mutate(Date = na.locf(as.Date(Date, origin = '1970-01-01'), na.rm=FALSE))
#     ID       Date
#  (int)     (date)
#1     1       <NA>
#2     1       <NA>
#3     1 2015-01-01
#4     1 2015-01-01
#5     2       <NA>
#6     2 2015-01-01
#7     2 2015-01-01
#8     2 2015-01-01
akrun
  • 874,273
  • 37
  • 540
  • 662