about reshaping a data frame
1
0
Entering edit mode
Bogdan ▴ 670
@bogdan-2367
Last seen 13 months ago
Palo Alto, CA, USA

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

maftools complexheatmap • 1.6k views
ADD COMMENT
2
Entering edit mode
Haiying.Kong ▴ 110
@haiyingkong-9254
Last seen 5.6 years ago
Germany

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 COMMENT
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 633 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6