[R] data formatting: from rows to columns

jim holtman jholtman at gmail.com
Wed Aug 29 01:11:19 CEST 2007


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?



More information about the R-help mailing list