0

I'm new in R. I'm workin with a data frame like this:

 > df
Site        Species              
A       Davilla kunthii             
A       Faramea occidentalis     
A       Thevetia ahouai          
A       Desmodium axillare       
B       Mimosa sp.               
B       Sida rhombifolia         
B       Sida rhombifolia         

I have multiples sites. In each of these sites, I need to calculate the proportion of every species, so I can obtain something like this:

output

Site        Species              Proportion
A       Davilla kunthii          0.25   
A       Faramea occidentalis     0.25
A       Thevetia ahouai          0.25
A       Desmodium axillare       0.25
B       Mimosa sp.               0.3333
B       Sida rhombifolia         0.6666

I've tried this:

    sites_split <- split(df, df$Site)

    x<-data.frame(sites_split$A %>%
    group_by(Species) %>%
    tally)
    
    y<-transform(x, Prop = n / sum(n))

but I'm looking for a way in which I don't have to create lots of objects, but I cannot find it. Could you please help me?

0 Answers0