Hello everyone
I have a problem that I think could be solved using pivot_wider, however I have not figured out how to do it. I have a dataset:
data <- data.frame(Person = c("Peter", "Peter", "Peter", "Peter", "Carol", "Carol", "Carol", "Carol"),
GroupID = c(1, 1, 2, 2, 3, 3, 4, 4),
Group = c("Scholar", "Scholar", "Zephyr", "Zephyr", "HOJ", "HOJ", "Colossus", "Colossus"),
Committee = c("Nature", "Transport", "Nature", "Transport", "State", "Transport", "State", "Transport"))
# Which looks like that:
Person GroupID Group Committee
1 Peter 1 Scholar Nature
2 Peter 1 Scholar Transport
3 Peter 2 Zephyr Nature
4 Peter 2 Zephyr Transport
5 Carol 3 HOJ State
6 Carol 3 HOJ Transport
7 Carol 4 Colossus State
8 Carol 4 Colossus Transport
I want to have one row per Person. For that, i need to widen the dataset by GroupID and group.
Note that the observations for "Committee" of a Person repeat for each Group. This is intended and is the case for every "Name" in the original dataset. Peter for example is in two committees and two groups.
The new dataset should as follows:
Person GroupID_1 Group_1 GroupID_2 Group_2 Committee_1 Committee_2
1 Peter 1 Scholar 2 Zephyr Nature Transport
2 Carol 3 HOJ 4 Colossus State Transport
Any help is massively appreciated!
(I suppose it is likely that there is already a solution somewhere out there, but a big problem with being new to R and programming language in general is that I am missing the lingo to search for the right keywords and find already existing solutions.)