I have a dataframe like so:
df<- data.frame(date= c(rep("10-29-16", 3), rep("11-14-16", 2),
"12-29-16","10-2-17","9-2-17"),
loc= c(rep("A", 3), rep("B", 2),"A","PlotA","PlotB"),
obs_network= c(rep("NA", 3), rep("NA", 2),"NA","PlotA","PlotB"))
For obs_network
which are NA
I want to give them a name for each unique date
and loc
combo. I would like the unique groups to be assigned a unique number and the prefix "pseudoplot" for this naming scheme. So the output would look like this:
output<- data.frame(date= c(rep("10-29-16", 3), rep("11-14-16", 2),
"12-29-16","10-2-17","9-2-17"),
loc= c(rep("A", 3), rep("B", 2),"A","PlotA","PlotB"),
obs_network= c(rep("pseudoplot_1", 3),rep("pseudoplot_2", 2),"pseudoplot_3","PlotA","PlotB"))
I have tried the following without success and I cannot identify my error. Using the code below all the levels read "pseudoplot1". I would greatly appreciate it if someone explained why my code is not working in addition to providing a solution.
output<-
df %>%
group_by(date, loc)%>%
mutate(obs_network=ifelse(is.na(obs_network),
paste0("pseudoplot", "_", match(loc, unique (loc))),
obs_network))