My data looks like this:
proposal_number <- c("Expt 1", "Expt 1", "Expt 1", "Expt 2", "Expt 2")
crop_weight <- c("Winter Wheat 200g", "Winter Barley 200g", "Spring Beans 500g", "Winter Wheat 300g", "Spring Beans 100g")
data<-data.frame(proposal_number, crop_weight)
proposal_number crop_weight
1 Expt 1 Winter Wheat 200g
2 Expt 1 Winter Barley 200g
3 Expt 1 Spring Beans 500g
4 Expt 2 Winter Wheat 300g
5 Expt 2 Spring Beans 100g
And I want to collapse the levels of crop_weight
by proposal_number
so it looks like this:
proposal_number crop_weight
1 Expt 1 Winter Wheat 200g, Winter Barley 200g, Spring Beans 500g
2 Expt 2 Winter Wheat 300g, Spring Beans 100
I have got this far:
help <-data %>%
group_by(proposal_number) %>%
mutate(crop_weight = paste(crop_weight, collapse = "; "))
But it pastes all the factor levels of crop_weight
for both factor levels of proposal_number
, like this:
proposal_number crop_weight
1 Expt 1 Winter Wheat 200g, Winter Barley 200g, Spring Beans 500g, Winter Wheat 300g, Spring Beans 100g
2 Expt 2 Winter Wheat 200g, Winter Barley 200g, Spring Beans 500g, Winter Wheat 300g, Spring Beans 100g
I've been googling it all morning and maybe my search terms are wrong, but I can't find an obvious answer. I'd be very grateful for any insights as to where I am going wrong?
This is the most useful thread I have found, but as I say above it doesn't quite work... Concatenate strings by group with dplyr
Many thanks