non-standard reshape from long to wide

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

non-standard reshape from long to wide

Yuan Chun Ding
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)

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: non-standard reshape from long to wide

Bert Gunter-2
Is this homework? There is a no-homework policy on this list.

If not, note that you are usually asked to show what you tried and the
error messages you received.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jan 7, 2021 at 10:40 AM Yuan Chun Ding <[hidden email]> wrote:

> 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)
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: non-standard reshape from long to wide

Yuan Chun Ding
Hi Bert,

No, this Is not home work related.  Original data have 87352 rows. I used the standard reshape function and got warning message. So I reformatted the wide format to meet my research purpose.

mut2 <-mut[,c("Tumor_Sample_Barcode","mut.id", "Hugo_Symbol")]
mut2 <-mut2[order(mut2$Hugo_Symbol),]
mut3 <-mut2[!duplicated(mut2),]
mut4 <-reshape(mut3, idvar = "Hugo_Symbol", timevar = "Tumor_Sample_Barcode", direction = "wide")

There were 50 or more warnings (use warnings() to see the first 50)
> View(mut4)
> warnings()
Warning messages:
1: In reshapeWide(data, idvar = idvar, timevar = timevar,  ... :
  multiple rows match for Tumor_Sample_Barcode=TCGA-A8-A09Z-01A-11W-A019-09: first taken
2: In reshapeWide(data, idvar = idvar, timevar = timevar,  ... :
From: Bert Gunter [mailto:[hidden email]]
Sent: Thursday, January 7, 2021 10:52 AM
To: Yuan Chun Ding <[hidden email]>
Cc: [hidden email]
Subject: Re: [R] non-standard reshape from long to wide

Is this homework? There is a no-homework policy on this list.

If not, note that you are usually asked to show what you tried and the error messages you received.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jan 7, 2021 at 10:40 AM Yuan Chun Ding <[hidden email]<mailto:[hidden email]>> wrote:
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 rec
 eive 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)

______________________________________________
[hidden email]<mailto:[hidden email]> mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help<https://urldefense.com/v3/__https:/stat.ethz.ch/mailman/listinfo/r-help__;!!Fou38LsQmgU!8Xh6f5YkEhmPfDyrfZAdBKkVH3I-iNCUoXNhMSZyF6JgRliIYBMD4tWItfuZ$>
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<https://urldefense.com/v3/__http:/www.R-project.org/posting-guide.html__;!!Fou38LsQmgU!8Xh6f5YkEhmPfDyrfZAdBKkVH3I-iNCUoXNhMSZyF6JgRliIYBMD4qvlyPfu$>
and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: non-standard reshape from long to wide

Bert Gunter-2
Show us your attempt on your example data. Also note that warnings are
*not* errors, though they typically do indicate problems.

-- Bert

On Thu, Jan 7, 2021 at 11:09 AM Yuan Chun Ding <[hidden email]> wrote:

> Hi Bert,
>
>
>
> No, this Is not home work related.  Original data have 87352 rows. I used
> the standard reshape function and got warning message. So I reformatted the
> wide format to meet my research purpose.
>
>
>
> mut2 <-mut[,c("Tumor_Sample_Barcode","mut.id", "Hugo_Symbol")]
>
> mut2 <-mut2[order(mut2$Hugo_Symbol),]
>
> mut3 <-mut2[!duplicated(mut2),]
>
> mut4 <-reshape(mut3, idvar = "Hugo_Symbol", timevar =
> "Tumor_Sample_Barcode", direction = "wide")
>
>
>
> There were 50 or more warnings (use warnings() to see the first 50)
>
> > View(mut4)
>
> > warnings()
>
> Warning messages:
>
> 1: In reshapeWide(data, idvar = idvar, timevar = timevar,  ... :
>
>   multiple rows match for
> Tumor_Sample_Barcode=TCGA-A8-A09Z-01A-11W-A019-09: first taken
>
> 2: In reshapeWide(data, idvar = idvar, timevar = timevar,  ... :
>
> *From:* Bert Gunter [mailto:[hidden email]]
> *Sent:* Thursday, January 7, 2021 10:52 AM
> *To:* Yuan Chun Ding <[hidden email]>
> *Cc:* [hidden email]
> *Subject:* Re: [R] non-standard reshape from long to wide
>
>
>
> Is this homework? There is a no-homework policy on this list.
>
>
>
> If not, note that you are usually asked to show what you tried and the
> error messages you received.
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
>
>
>
> On Thu, Jan 7, 2021 at 10:40 AM Yuan Chun Ding <[hidden email]> wrote:
>
> 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)
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> <https://urldefense.com/v3/__https:/stat.ethz.ch/mailman/listinfo/r-help__;!!Fou38LsQmgU!8Xh6f5YkEhmPfDyrfZAdBKkVH3I-iNCUoXNhMSZyF6JgRliIYBMD4tWItfuZ$>
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> <https://urldefense.com/v3/__http:/www.R-project.org/posting-guide.html__;!!Fou38LsQmgU!8Xh6f5YkEhmPfDyrfZAdBKkVH3I-iNCUoXNhMSZyF6JgRliIYBMD4qvlyPfu$>
> and provide commented, minimal, self-contained, reproducible code.
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: non-standard reshape from long to wide

Rui Barradas
In reply to this post by Yuan Chun Ding
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:

> 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)
>
> ______________________________________________
> [hidden email] 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.
>
______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: non-standard reshape from long to wide

Yuan Chun Ding
Hi Rui,

Thank you so much!!   You code works well and I am looking into the pivot_wider function.

Yuan Ding

-----Original Message-----
From: Rui Barradas [mailto:[hidden email]]
Sent: Thursday, January 7, 2021 12:19 PM
To: Yuan Chun Ding <[hidden email]>; [hidden email]
Subject: Re: [R] 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:

> 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)
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!Fou38LsQmgU!9ccbhtYzBJoahdschhouzo2kkluOs-EdoH8jn32fv9E22xaJ4GzfrI0bOvVl$ 
> PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!Fou38LsQmgU!9ccbhtYzBJoahdschhouzo2kkluOs-EdoH8jn32fv9E22xaJ4GzfrKjC2095$ 
> and provide commented, minimal, self-contained, reproducible code.
>
______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: non-standard reshape from long to wide

PIKAL Petr
In reply to this post by Yuan Chun Ding
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 <[hidden email]> On Behalf Of Yuan Chun Ding
> Sent: Thursday, January 7, 2021 7:40 PM
> To: [hidden email]
> 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)
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.