-1

enter image description here

I have data set that i put a picture of. I want to create 4 new groups that will consist of different transects. On the picture you can see different letters and nunmbers so each habitat has a letter and consists of 4 samplins points. So what i want is that each habitat has the values from their transect.

As an example I want to have habitats: Torre,Bolsa,Amable,Eduardo and for examples Torre would have values from T1-T4, Bolsa B1-B4, Amable A1-A4 etc. How can I create such groups?

Thank you very much for your help!

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please [do not post code or data in images](https://meta.stackoverflow.com/q/285551/2372064) – MrFlick Feb 08 '23 at 15:02
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 08 '23 at 21:12

1 Answers1

0

There is a possibility using ifelse() and str_detect(), supposing you have only 4 categories and no empty values :

library(dplyr)
library(stringr)
    data %>% mutate(Habitat = ifelse(str_detect(Transect,"A"),"Amable",
                                ifelse(str_detect(Transect,"B"),"Bolsa",
                                       ifelse(str_detect(Transect,"E"),"Eduardo",
                                             "Torre"))))
Basti
  • 1,703
  • 3
  • 10
  • 25