Hi Ding,
If you are still having trouble, perhaps:

library(prettyR)
stretch_df(test1,"vntr1","val")

Jim

On Sat, Apr 4, 2020 at 5:58 AM Yuan Chun Ding <ycd...@coh.org> wrote:
>
> Hi R users,
>
> I want to do a data reshape from long to wide, I thought it was easy using 
> tidyverse spread function, but it did not work well. Can you help me?
>
> Thank you,
>
> Ding
>
> test1 data frame is long file and test2 is the wide file I want to get
>
> test1 <- data.frame (vntr1=c("v1","v1", "v2","v2","v2","v2"),
>                      val =c(0.98,0.02, 0.59,0.12,0.11,0.04))
>
> test2  <- data.frame(vntr1=c("v1","v2"),
>                      a1 =c(0.98, 0.5693),
>                      a2 = c(0.02, 0.12),
>                      a3 =c(NA, 0.11),
>                      a4=c(NA, 0.04))
>
> the following code does not work
> test2 <-test1 %>%spread(vntr1, val)
>
>  Error: Each row of output must be identified by a unique combination of keys.
> Keys are shared for 6 rows:
> * 1, 2
> * 3, 4, 5, 6
> Do you need to create unique ID with tibble::rowid_to_column()?
> Call `rlang::last_error()` to see a backtrace
>
> ----------------------------------------------------------------------
> ------------------------------------------------------------
> -SECURITY/CONFIDENTIALITY WARNING-
>
> This message and any attachments are intended solely for the individual or 
> entity to which they are addressed. This communication may contain 
> information that is privileged, confidential, or exempt from disclosure under 
> applicable law (e.g., personal health information, research data, financial 
> information). Because this e-mail has been sent without encryption, 
> individuals other than the intended recipient may be able to view the 
> information, forward it to others or tamper with the information without the 
> knowledge or consent of the sender. If you are not the intended recipient, or 
> the employee or person responsible for delivering the message to the intended 
> recipient, any dissemination, distribution or copying of the communication is 
> strictly prohibited. If you received the communication in error, please 
> notify the sender immediately by replying to this message and deleting the 
> message and any accompanying files from your system. If, due to the security 
> risks, you do not wish to r
 eceive further communications via e-mail, please reply to this message and 
inform the sender that you do not wish to receive further e-mail from the 
sender. (LCP301)
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to