This is obviously an entry level question but just seems to indicate I might be missing some fundamentals of how R handles data transposition on a basic level...
Note, I have been reading all other messages/replies but I am still confused over why I couldn't perform this very simple task below .
Could someone at least explain to me what's happening here, also how to use those options in the warning message? (fairly straightforward to do in normal circumstances with 3 columns or more so something is missing here..)
My intention is to first create a data set of 2 columns with 100 records and 10 levels per fn variable, then transpose it into a wide dataset with 10 columns.
fn <- seq(1,10)
val <- rnorm(100,0,1)
datout <- data.frame(fn,val)
##no duplicate
> which(duplicated(datout))
integer(0)
##pivot_wider gave me a listing with warning message:
datwd <- datout %>%
mutate(fn=paste0("type",fn)) %>%
pivot_wider(names_from=fn, values_from=val)
##only getting a listing with warning message:
A tibble: 1 x 10
type1 type2 type3 type4 type5 type6 type7 type8 type9 type10
1 <dbl [10]> <dbl [10]> <dbl [10]> <dbl [10]> <dbl [10]> <dbl [10]> <dbl [10]> <dbl [10]> <dbl [10]> <dbl [10]>
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 ari**se - Use
values_fn = {summary_fun}
to summarise duplicates
##spread() won't work at all
> datout %>%
+ spread("fn", "val")
Error: Each row of output must be identified by a unique combination of keys.