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...
Cheers,
Fede
--
Federico C. F. Calboli
Department of Epidemiology and Public Health
Imperial College, St Mary's Campus
Norfolk Place, London W2 1PG
Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193
f.calboli [.a.t] imperial.ac.uk
f.calboli [.a.t] gmail.com
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
Here is a way using sprintf:
x <- read.table(textConnection(" 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"))
# output the data
cat(sprintf("%d\n%d\n\n", x$V2, x$V3), sep='',
file='tempxx.txt')
On 8/28/07, Federico Calboli <f.calboli at imperial.ac.uk>
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...
>
> Cheers,
>
> Fede
>
>
>
>
>
>
> --
> Federico C. F. Calboli
> Department of Epidemiology and Public Health
> Imperial College, St Mary's Campus
> Norfolk Place, London W2 1PG
>
> Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193
>
> f.calboli [.a.t] imperial.ac.uk
> f.calboli [.a.t] gmail.com
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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 you are trying to solve?
At 09:11 29/08/07 Jim Holtman wrote
Here is a way using sprintf:
x <- read.table(textConnection(" 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"))
# output the data
cat(sprintf("%d\n%d\n\n", x$V2, x$V3), sep='',
file='tempxx.txt')
How about
write.table(x,"clipboard",sep="\n",eol="\n\n",col.names
= FALSE, row.names
= FALSE)
2032567
19
2035482
19
2472826
19
2473320
19
2035480
135
2062458
135
2074927
135
2102395
142
2027252
142
2473082
142
Regards
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: dmackay9 at pobox.une.edu.au
Ph 02 67729794