[R] non-standard reshape from long to wide

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Fri Jan 8 10:57:38 CET 2021


Hi

dcast from reshape is close, however column order is different

mydf <- dcast(df.long, sample~marker)
(!is.na(mydf[,-1]))*1
     g j k u x y
[1,] 1 0 1 0 1 0
[2,] 0 1 0 1 1 1
[3,] 0 0 0 0 1 1

You just need to change 0 to NA and add rownames from mydf.

Cheers
Petr

> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Yuan Chun Ding
> Sent: Thursday, January 7, 2021 7:40 PM
> To: r-help using r-project.org
> Subject: [R] non-standard reshape from long to wide
> 
> Dear R user,
> 
> I want to reshape a long data frame to wide format, I made the following
> example files.  Can you help me?
> 
> Thank you,
> 
> Yuan Chun Ding
> 
> sample <-c("xr" , "xr" , "fh" , "fh" , "fh" , "uy" , "uy" , "uy" , "uy");
marker <-
> c("x" , "y" , "g" , "x" , "k" , "y" , "x" , "u" , "j"); df.long
<-data.frame(sample,
> marker);
> 
> xr <-c(1,1,NA,NA,NA,NA);
> fh <-c(1,NA,1,1,NA,NA);
> uy <-c(1,1,NA,NA,1,1);
> 
> df.wide <- t(data.frame(xr,fh,uy));
> colnames(df.wide)<-c("x","y","g","k", "u","j");
> 
> ----------------------------------------------------------------------
> ------------------------------------------------------------
> -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 receive 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 using 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.


More information about the R-help mailing list