I want to insert 'NA' value for specified Plot, time and Date and they are located randomly. I figure out how to do manually using add_row
function but the main concern for me is I have tons of data and doing manually doesn't help. My data are in this format.
Plot Date Time Canopyheight
B1 10/22/2019 22 50
B1 10/22/2019 1 80
B1 10/22/2019 4 9
So my in each plot, there are 4 timestamps as 22,1,4 and 6 and sometimes there is missing time stamp like B1 10/22/2019 6 Na
. I can add these rows by using the code below
add_row(agg, Date = '10/21/2019', Plot = 'BG107B2', Time = 22,
Canopyheight = NA, .before = 1)
but I have several dates and plots where I need to add the row. I have tried the following codes
test <- agg %>%
mutate(ID2 = as.integer(factor(Plot, levels = unique(.$Plot)))) %>%
split(f = .$ID2) %>%
map_if(.p = function(x) unique(x$ID2) != unique(last(.)$ID2),
~bind_rows(.x, tibble(Time = unique(.x$Time), Canopyheight = NA,
ID2 = unique(.x$ID2)))) %>%
bind_rows() %>%
select(-ID2)
But still, I am not able to do it, is there any ways that I can automate this instead of the manual way?
Thanks and have a great day.