I'm trying to figure out a way to group data, and then create a column based on the content of the grouped rows.
Sample df
to be manipulated
df <- tibble::tribble(
~name, ~position, ~G,
"DJ LeMahieu", "1B", 40,
"DJ LeMahieu", "2B", 75,
"DJ LeMahieu", "3B", 52,
"Max Muncy", "1B", 65,
"Max Muncy", "2B", 70,
"Max Muncy", "3B", 35,
"Whit Merrifield", "2B", 82,
"Whit Merrifield", "OF", 61
)
I then want this content to be grouped at the name level. I want to create a new column called extra_position. This column would be a concatenate of the content in the position column separated by a "/". Example output below:
output_df <- tibble::tribble(
~name, ~extra_position,
"DJ LeMahieu", "1B/2B/3B",
"Max Muncy", "1B/2B/3B",
"Whit Merrifield", "2B/OF"
)
I'd like to stay within the tidyverse
if possible. In addition, I'm curious to know whether you can also control the order of the data being concatenated. For example, can you make DJ LeMahieu's extra_position
content show as: "3B/2B/1B"
?