Re: [R] help with reshape is needed again!

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Fri, 18 Jun 2010 14:14:06 -0700

Hi:

# Method 1: Transposition and name corrections
> dd <- read.table(textConnection("

+ gene1   breast   10            100             1
+ gene2   breast   20            200             4
+ gene3   breast   30            50              5
+ gene4   breast   40            400             9"))

> closeAllConnections()
> dm <- as.matrix(dd[ , 3:5])
> rownames(dm) <- dd[, 1]
> dm2 <- as.data.frame(t(dm))
> dm2$patient <- rownames(dm2)
> dm2 <- dm2[, c(5, 1:4)]
> dm2

  patient gene1 gene2 gene3 gene4
1       1    10    20    30    40
2       2   100   200    50   400
3       3     1     4     5     9

# Method 2: package reshape
# Using dd as above,

> library(reshape)
> names(dd) <- c('gene', 'type', 'patient1', 'patient2', 'patient3')
> df2 <- melt(dd[, -2], idvar = 'gene')
> df2$patient <- gsub('[[:alpha:]]', '', df2$variable)
> cast(df2, patient ~ gene)

  patient gene1 gene2 gene3 gene4
1 1 10 20 30 40
2 2 100 200 50 400
3 3 1 4 5 9

HTH,
Dennis

On Fri, Jun 18, 2010 at 12:07 PM, xin wei <xinwei_at_stat.psu.edu> wrote:

>
> hi, folks:
> i need to transpose the following data:
>
> gene tissue patient1 patient2 patient3.....
> ---------------------------------------------
> gene1 breast 10 100 1
> gene2 breast 20 200 4
> gene3 breast 30 50 5
> gene4 breast 40 400 9
> ................................
>
> to the following format:
>
> patientID gene1 gene2 gene3 gene4............
> -------------------------------------------
> 1 10 20 30 40
> 2 100 200 50 400
> 3 1 4 5 9
>
> any suggestions how to use reshape or other function to achieve this?
>
> thanks
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/help-with-reshape-is-needed-again-tp2260640p2260640.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> 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]]



R-help_at_r-project.org mailing list
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. Received on Fri 18 Jun 2010 - 21:19:41 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 19 Jun 2010 - 08:40:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive