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 <xin...@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@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@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.

Reply via email to