[R] R loop.

Gustaf Rydevik gustaf.rydevik at gmail.com
Fri Apr 23 10:11:32 CEST 2010


On Thu, Apr 22, 2010 at 7:20 PM, mhalsham <mhalsham at bradford.ac.uk> wrote:
>
> Ok sorry for bad explanation from my side
> What I want. I have a txt file name is (table3.txt) this file contains 1293
> rows and some of these row will have 1 column and some of them will have up
> to may be 40 column. For example
>
>        A                     B C       D       E       F       G       H       I
> 1       Deafness                    EYA4        MYO7A   TECTA   COL11A2 POU4F3  MYH9    ACTG1   MYO6
> 2       Leukemia                     TAL1       TAL2    ZNFN1A1 FLT3
> 3       Colon_cancer    RAD54B  PTPN12  BCL10
>
> The orders below will show how I want the recorders to be.
>        A              B
> 1       Deafness            EYA4
> 2       Deafness              MYO7A
> 3       Deafness             TECTA
> 4       Deafness            COL11A2
> 5       Deafness            POU4F3
> 6       Deafness             MYH9
> 7       Deafness             ACTG1
> 8       Deafness              MYO6
> 9       Leukemia              TAL1
> 10      Leukemia              TAL2
> 11      Leukemia                ZNFN1A1
> 12      Leukemia        FLT3
> 13      Colon_cancer    RAD54B
> 14      Colon_cancer    PTPN12
> 15      Colon_cancer    BCL10
>
> Any help will very kind of every one, and thanks for those who trying to
> help and couldn’t understand me. Thank you
>
>


Have you managed to read your table3.txt into R,using read.table etc?
If so, could you copy/paste the result of using dput() on your object?


After a bit of work, I've gotten your example data into R, but please
post either comma-separated data or dput() results in the
future.Anyhow, here's an example of how to get what you want. Hope it
helps.
Regards,
Gustaf
..........................

example.data<-structure(list(V1 = structure(c(2L, 3L, 1L), .Label =
c("Colon_cancer",
"Deafness", "Leukemia"), class = "factor"), V2 = structure(c(1L,
3L, 2L), .Label = c("EYA4", "RAD54B", "TAL1"), class = "factor"),
    V3 = structure(c(1L, 3L, 2L), .Label = c("MYO7A", "PTPN12",
    "TAL2"), class = "factor"), V4 = structure(c(2L, 3L, 1L), .Label =
c("BCL10",
    "TECTA", "ZNFN1A1"), class = "factor"), V5 = structure(c(2L,
    3L, 1L), .Label = c("", "COL11A2", "FLT3"), class = "factor"),
    V6 = structure(c(2L, 1L, 1L), .Label = c("", "POU4F3"), class = "factor"),
    V7 = structure(c(2L, 1L, 1L), .Label = c("", "MYH9"), class = "factor"),
    V8 = structure(c(2L, 1L, 1L), .Label = c("", "ACTG1"), class = "factor"),
    V9 = structure(c(2L, 1L, 1L), .Label = c("", "MYO6"), class =
"factor")), .Names = c("V1",
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9"), class = "data.frame",
row.names = c(NA,
-3L))

library(reshape)
example.long<-melt(exampledata,id.vars="V1")
example.long
#             V1 variable   value
#1      Deafness       V2    EYA4
#2      Leukemia       V2    TAL1
#3  Colon_cancer       V2  RAD54B
#4      Deafness       V3   MYO7A
#5      Leukemia       V3    TAL2
#6  Colon_cancer       V3  PTPN12
#7      Deafness       V4   TECTA
#8      Leukemia       V4 ZNFN1A1
#9  Colon_cancer       V4   BCL10
#10     Deafness       V5 COL11A2
#11     Leukemia       V5    FLT3
#12 Colon_cancer       V5
#13     Deafness       V6  POU4F3
#14     Leukemia       V6
#15 Colon_cancer       V6
#16     Deafness       V7    MYH9
#17     Leukemia       V7
#18 Colon_cancer       V7
#19     Deafness       V8   ACTG1
#20     Leukemia       V8
#21 Colon_cancer       V8
#22     Deafness       V9    MYO6
#23     Leukemia       V9
#24 Colon_cancer       V9
##Or if you want it in the order of V1
example.long[order(example.long$V1),]
#        V1 variable   value
#3  Colon_cancer       V2  RAD54B
#6  Colon_cancer       V3  PTPN12
#9  Colon_cancer       V4   BCL10
#12 Colon_cancer       V5
#15 Colon_cancer       V6
#18 Colon_cancer       V7
#21 Colon_cancer       V8
#24 Colon_cancer       V9
#1      Deafness       V2    EYA4
#4      Deafness       V3   MYO7A
#7      Deafness       V4   TECTA
#10     Deafness       V5 COL11A2
#13     Deafness       V6  POU4F3
#16     Deafness       V7    MYH9
#19     Deafness       V8   ACTG1
#22     Deafness       V9    MYO6
#2      Leukemia       V2    TAL1
#5      Leukemia       V3    TAL2
#8      Leukemia       V4 ZNFN1A1
#11     Leukemia       V5    FLT3
#14     Leukemia       V6
#17     Leukemia       V7
#20     Leukemia       V8
#23     Leukemia       V9


-- 
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik



More information about the R-help mailing list