I have a dataframe like so:
data<- data.frame(ID= seq(1,12, 1),
plantfam= c(1,1,2,2,1,1,1,1,2,2,3,3),
lepsp= c(rep("A", 4), "B", "B", rep("C", 6)),
needsmorpho= c(rep("yes", 4),"no", "no", rep("yes", 6)))
I need to first filter all needsmorpho that are yes. Then I need to group all lepsp with the same plantfam. For each unique lepsp and plantfam match the lepsp will be given a unique morpho species name. To make a morphosp name I would like to paste morphosp and a unique number based on the unique lepsp and plantfam matches. The output would be:
output<- data.frame(ID= seq(1,12, 1),
plantfam= c(1,1,2,2,1,1,1,1,2,2,3,3),
lepsp= c("A_morpho1","A_morpho1","A_morpho2","A_morpho2",
"B","B","C_morpho1","C_morpho1",
"C_morpho2","C_morpho2","C_morpho3","C_morpho3"),
needsmorpho= c(rep("yes", 4),"no", "no", rep("yes", 6)))
I have tried:
subset1 <-
file %>%
filter(NeedsMorpho == "yes") %>%
group_by(lepsp) %>%
mutate(lepsp =
paste0(lepsp,"_morphosp",match(plantfam,unique(plantfam))))
subset2 <-
file %>%
filter(NeedsMorpho == "yes") %>%
setdiff(file, .)
file<-union(subset1, subset2) %>% arrange(lepsp)