I'm attempting to widen a dataframe from an existing one, while keeping an extra column attached, but am receiving list-columns instead of spread out cells.
I have a large dataset I am attempting to widen:
Location | Names | Result |
---|---|---|
A | Foo | 1 |
A | Bar | 1.5 |
B | Foo | 2 |
B | Foo | 1 |
C | Bar | 2.5 |
D | Baz | 0.5 |
I attempted:
Subset <- Data %>%
group_by(Names) %>%
pivot_wider(
names_from = Names,
values_from = Result,
id_cols = Location
)
And receive something like this:
Location | Foo | Bar | Baz |
---|---|---|---|
A | 1 | 1.5 | etc. |
B | c(2,1) | etc. | etc. |
C | NA | etc. | etc. |
With this warning:
Values from Result
are not uniquely identified; output will contain
list-cols.
• Use values_fn = list
to suppress this warning.
• Use values_fn = {summary_fun}
to summarise duplicates.
• Use the following dplyr code to identify duplicates.
{data} %>%
dplyr::group_by(Location, Names) %>%
dplyr::summarise(n = dplyr::n(), .groups = "drop") %>%
dplyr::filter(n > 1L)
I am wondering if there is any way to spread out the combined values in 1 cell to multiple rows to be something like this, but be able to perform this on many rows at once, as the dataframe is over 50 rows long.
Location | Foo | Bar | Baz |
---|---|---|---|
A | 1 | 1.5 | |
B | 2 | etc. | |
B | 1 | ||
C | NA |
Any way to do this within pivot_wider, or another function? Thank you!