Please give an example input data.frame and desired output. Use the reprex package to paste code if you want faster answers.
BTW you have incorrectly tagged this question to Rstudio IDE
.
It is not an IDE question. It is a general R question. Please replace the tag too.
Thanks
SM