I am not able to convert data from long to wide format following the examples in this portal (R how to convert from long to wide format, Converting long format to wide format, converting a long-formated dataframe to wide format tidyverse). I am not sure what am I missing. I am trying to transform the long data frame to a wide format as shown below:
library(tidyr)
Y <- c("A","A","A","A","A","A","B","B","B","C","C","C","C","C","C","C","C","D","D","D")
Z <- c("ABC","BCD","CDE","DEF","EFG","FGH","A12","B12","C12","A45","B45","C45","D45","E45","F45","G45","H45","X66","Y66","Z66")
df <- as.data.frame(cbind(Y,Z))
data_wide <- spread(df, Y, Z)
Error: Each row of output must be identified by a unique combination of keys.
Keys are shared for 20 rows:
* 1, 2, 3, 4, 5, 6
* 7, 8, 9
* 10, 11, 12, 13, 14, 15, 16, 17
* 18, 19, 20
library(tidyverse)
data_wide <- pivot_wider(df, names_from = Y, values_from = Z, values_fill = "")
Error: Can't convert <character> to <list>.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
Values are not uniquely identified; output will contain list-cols.
* Use `values_fn = list` to suppress this warning.
* Use `values_fn = length` to identify where the duplicates arise
* Use `values_fn = {summary_fun}` to summarise duplicates