Question: about reshaping a data frame
0
gravatar for Bogdan
2.1 years ago by
Bogdan570
Palo Alto, CA, USA
Bogdan570 wrote:

Dear all,

although it is a more general R question, if you do not mind having me asking the question on BioC list, as it relates to the packages maftools and ComplexHeatmap.  

I would appreciate please a piece of help regarding the use of acast/dcast functions in reshape2 package (the output matrix will serve as input into maftools and ComplexHeatmap). 

Specifically, I'm working with a data frame, that has information about SAMPLE, GENE, and TYPE of MUTATION (as shown below):

Sample    Gene  Type
22M       AEBP1   SNV
17M       AEBP1   SNV
22M         ATR   INDEL
22M         ATR   SNV
11M         BTK   SNV
11M         BTK INDEL

I would like to transform this DATAFRAME into a MATRIX that has :

-- GENE on ROWS,

-- SAMPLE on COLUMNS,

-- and the elements of the matrix are SNV or INDEL (ie the types of mutations). 

The R code starts with :

y <- data.frame(Sample = x$Sample, Gene = x$Gene, Type=x$Type) 

z <- acast(y, Cancer_Gene ~ Sample)

although in z, I do not have the information on Type (i.e.SNV or INDEL).

thanks a lot, 

-- bogdan

complexheatmap maftools • 449 views
ADD COMMENTlink modified 2.1 years ago by Haiying.Kong110 • written 2.1 years ago by Bogdan570
Answer: about reshaping a data frame
2
gravatar for Haiying.Kong
2.1 years ago by
Haiying.Kong110
Germany
Haiying.Kong110 wrote:

Because there are redundancies:

22M         ATR   INDEL
22M         ATR   SNV

  and
11M         BTK   SNV
11M         BTK INDEL

you cannot do reshaping on the original data.

 you need to collapse the data to something like:

Sample    Gene  Type
22M       AEBP1   SNV
17M       AEBP1   SNV
22M         ATR   INDEL,SNV
11M         BTK   SNV,INDEL

  This can be done with:
b = aggregate(y[ ,-(1:2)], by=list(y$Sample, y$Gene), FUN=paste)

names(b) = names(Y)

Then, you can do reshaping with:

wide = reshape(b, idvar="Gene", timevar="Sample", direction="wide")

  You can get more information from :

https://stats.idre.ucla.edu/r/faq/how-can-i-reshape-my-data-in-r/

 

ADD COMMENTlink modified 2.1 years ago • written 2.1 years ago by Haiying.Kong110

Dear Haiying, thank you very much for suggestions and kind help ;)

ADD REPLYlink written 2.1 years ago by Bogdan570
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 209 users visited in the last hour