I have a dataframe on bat activity with variables
"plot_no" "date" "rec_hour" "guild" "sunrise" "sunriseEnd" "sunsetStart" "sunset" "count" "id"
where ID is a character combination of plot_no, date, rec_hour and guild.
At the moment, if there were no observations for a certain plot_no, date, time and guild, there is no line in the dataframe. I would like to have all relevant combinations of those variables and have a count of zero when there was no observation.
In order to do that, I made a new dataframe with all combinations and all zeros for count. Now I want to add the right count into the zero dataframe using this code:
for (nr in dim(Bat_Data_zeros_)[1]) {
if (dim(Bat_Data_count[Bat_Data_zeros_$id[nr] == Bat_Data_count$id,])[1]>0) {
Bat_Data_zeros_$count[nr] <- Bat_Data_count$count[Bat_Data_zeros_$id[nr] == Bat_Data_count$id]
}
}
but only one value (not even the first one) gets replaced. I checked every part of the code separately to see if it gave the output I was expecting and it all seemed right. I also tried using str_detect (was my first attempt) on the IDs, but the str_detect function for some reason gives back way more matches than there actually are.
tidyr::completewill do this for you?