Skip to content
Prev 386779 / 398502 Next

non-standard reshape from long to wide

Hello,

Here is a dplyr solution. The main trick is to create a column of 1's, 
then pipe to pivot_wider.


library(dplyr)
library(tidyr)

df.long %>%
   mutate(values = 1) %>%
   pivot_wider(
     id_cols = sample,
     names_from = marker,
     values_from = values,
     values_fill = NA
   )


Note: your df.wide is not a data.frame, the transpose coerces it to 
matrix. In this case it doesn't matter because it was just an example of 
expected output but in other, real use cases you must be careful.

df.wide <- as.data.frame(df.wide)

would solve it.


Hope this helps,

Rui Barradas

?s 18:39 de 07/01/21, Yuan Chun Ding escreveu: