Reputation: 545
I have a data frame that looks this.
df <- data.frame(names = c("Ram","Siddhharth","Nithin","Aashrit","Bragadish","Sridhar"),
house = c("A", "B", "A", "B", "A", "B"))
I want to create a new data frame which gets re-arranged based on the house they are in.
Expected Output
house_A house_B
1 Ram Siddhharth
2 Nithin Aashrit
3 Bragadish Sridhar
How can I achieve this? Many thanks in advance.
Upvotes: 0
Views: 45
Reputation: 16978
You could use tidyr
:
df %>%
pivot_wider(names_from="house", names_prefix="house_", values_from="names", values_fn=list) %>%
unnest(cols=everything())
This returns
# A tibble: 3 x 2
house_A house_B
<chr> <chr>
1 Ram Siddhharth
2 Nithin Aashrit
3 Bragadish Sridhar
Upvotes: 3