Skip to content

Adding a column to a dataframe #28

@Kirstensos

Description

@Kirstensos

Hello Stirling coding club,

I am having difficulties with a data organisation problem and thought I would try out Github to see if anyone can help me out.

I have data in the following format:

plot | spp | value 
1    | 1   | 0.1    
1    | 2   | 0.2    
1    | 3   | 0.3   
2    | 2   | 0.1    
2    | 3   | 0.2    
3    | 1   | 0.3    
3    | 2   | 0.1    
3    | 3   | 0.2    
3    | 4   | 0.3    

I want to calculate the sum of values for each plot, then use this to calculate the percentage contribution of each spp in each plot. I am trying to make a data.frame something similar to this:

plot | spp | value| total| % of total
1    | 1   | 0.1  | 0.6  | 16.7
1    | 2   | 0.2  | 0.6  | 33.3
1    | 3   | 0.3  | 0.6  | 50
2    | 2   | 0.1  | 0.3  | 33.3
2    | 3   | 0.2  | 0.3  | 66.6
3    | 1   | 0.3  | 0.9  | 33.3
3    | 2   | 0.1  | 0.9  | 11.1
3    | 3   | 0.2  | 0.9  | 22.2
3    | 4   | 0.3  | 0.9  | 33.3

I think I need to modify something like this to incorporate a way of sorting by plot:

data$total<- sum(data$value)

I have also tried aggregate() but can't seem to make it work:

data$total<- aggregate(data$value, by=list(data$plot), FUN=sum)

Any ideas would be really helpful.
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions