[R] loop

jim holtman jholtman at gmail.com
Wed Jun 2 02:27:42 CEST 2010


try this:

> x <- readLines(textConnection("V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
+ TP53 Dis1 Dis2 Dis3 Dis4 Dis5 Dis6
+ DCI New1 New2 New3 New4
+ FDI Hi2 H3 H4
+ GHD I1 I3 I4 I5 I6 I7 I8"))[-1]  # drop the first line
> output <- do.call(rbind, lapply(x, function(.line){
+     # split the data
+     parms <- strsplit(.line, '\\s+')[[1]]
+     # create the indices
+     indx <- combn(length(parms) - 1, 2)
+     # now create the output
+     cbind(parms[1], parms[indx[1,] + 1], parms[indx[2,] + 1])
+ }))
>
>
>
> output
      [,1]   [,2]   [,3]
 [1,] "TP53" "Dis1" "Dis2"
 [2,] "TP53" "Dis1" "Dis3"
 [3,] "TP53" "Dis1" "Dis4"
 [4,] "TP53" "Dis1" "Dis5"
 [5,] "TP53" "Dis1" "Dis6"
 [6,] "TP53" "Dis2" "Dis3"
 [7,] "TP53" "Dis2" "Dis4"
 [8,] "TP53" "Dis2" "Dis5"
 [9,] "TP53" "Dis2" "Dis6"
[10,] "TP53" "Dis3" "Dis4"
[11,] "TP53" "Dis3" "Dis5"
[12,] "TP53" "Dis3" "Dis6"
[13,] "TP53" "Dis4" "Dis5"
[14,] "TP53" "Dis4" "Dis6"
[15,] "TP53" "Dis5" "Dis6"
[16,] "DCI"  "New1" "New2"
[17,] "DCI"  "New1" "New3"
[18,] "DCI"  "New1" "New4"
[19,] "DCI"  "New2" "New3"
[20,] "DCI"  "New2" "New4"
[21,] "DCI"  "New3" "New4"
[22,] "FDI"  "Hi2"  "H3"
.....

On Tue, Jun 1, 2010 at 5:12 AM, mhalsham <mhalsham at bradford.ac.uk> wrote:
>
> Can any one help it will be very kind, loop statements
> I have this table and some more records, I want to reshape it
>
> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
> TP53 Dis1 Dis2 Dis3 Dis4 Dis5 Dis6
> DCI New1 New2 New3 New4
> FDI Hi2 H3 H4
> GHD I1 I3 I4 I5 I6 I7 I8
>
> I want my new table or matrix to be some thing like this
>
> V1 V2 V3
> Tp53 Dis1 Dis2
> Tp53 Dis1 Dis3
> Tp53 Dis1 Dis4
> Tp53 Dis1 Dis5
> Tp53 Dis1 Dis6
> Tp53 Dis2 Dis3
> Tp53 Dis2 Dis4
> Tp53 Dis2 Dis5
> Tp53 Dis2 Dis6
> Tp53 Dis3 Dis4
> Tp53 Dis3 Dis5
> Tp53 Dis3 Dis6
> Tp53 Dis4 Dis5
> Tp53 Dis4 Dis6
> Tp53 Dis5 Dis6
> DCI New1 New2
> DCI New1 New3
> DCI New1 New4
> DCI New2 New3
> DCI New2 New4
> DCI New3 New4
>
>
> And so on for the rest of the table.
> --
> View this message in context: http://r.789695.n4.nabble.com/loop-tp2238209p2238209.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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list