[R] data formatting: from rows to columns
James W. MacDonald
jmacdon at med.umich.edu
Tue Aug 28 20:44:05 CEST 2007
Hi Federico,
Federico Calboli wrote:
> Hi All,
>
> I have some data I need to write as a file from R to use in a different program.
> My data comes as a numeric matrix of n rows and 2 colums, I need to transform
> each row as a two rows 1 col output, and separate the output of each row with a
> blanck line.
>
> Foe instance I need to go from this:
>
> V2 V3
> 27 2032567 19
> 28 2035482 19
> 126 2472826 19
> 132 2473320 19
> 136 2035480 135
> 145 2062458 135
> 148 2074927 135
> 151 2102395 142
> 156 2027252 142
> 158 2473082 142
>
> to
>
> 2032567
> 19
>
> 2035482
> 19
>
> 2472826
> 19
>
> 2473320
> 19
>
> 2035480
> 135
>
> ...
>
> Any hint? I seem a bit stuck. cat(unlist(data), file ='data.txt', sep = '\n')
> (obviously) does not work...
Likely somebody will come up with something better, but this works. Say
the matrix above is called 'mat':
con <- file("filename.txt", "w")
for(i in 1:dim(mat)[1]) cat(paste(mat[i,], collapse="\n"), "\n\n", file=con)
close(con)
Best,
Jim
>
> Cheers,
>
> Fede
>
>
>
>
>
>
--
James W. MacDonald, M.S.
Biostatistician
Affymetrix and cDNA Microarray Core
University of Michigan Cancer Center
1500 E. Medical Center Drive
7410 CCGC
Ann Arbor MI 48109
734-647-5623
More information about the R-help
mailing list