[R] Read data in sequences

Gabor Grothendieck ggrothendieck at gmail.com
Sat Apr 10 19:54:59 CEST 2010


Try this.  The data portion DF[-1] is transposed forming length 2
columns composed of successive elements of DF rows.  This is then
transposed back and the id's added in:

# read in
Lines <- "A 2.5 3.4 2.7 5.6 5.7 5.4 10.1 9.4
 B 5.3 5.4 6.5 7.5 1.3 4.5 10.5 4.1"
DF <- read.table(textConnection(Lines))

# form required data frame
data.frame(id = DF[gl(2,4),1], t(matrix(t(DF[-1]), 2)))



On Fri, Apr 9, 2010 at 11:25 AM, RockO <rock.ouimet at gmail.com> wrote:
>
> Dear R users,
>
> I tried to find a solution in the search list, but I cannot find it. I would
> like to read a .txt file with, let say, three variables, with two of which
> have repeated values in a number a columns.
>
> An example:
>
> The variables: Treat, x1, x2.
> The values:
> A 2.5 3.4 2.7 5.6 5.7 5.4 10.1 9.4 ...
> B 5.3 5.4 6.5 7.5 1.3 4.5 10.5 4.1 ...
> ...
>
> In the first column, the letters represent the variable "Treat", and the
> sequence of numbers on a same line represent pairs of values for "x1" and
> "x2".
>
> In SAS, this type of dataset is easy to read using "@@" as in:
> data a;
> input Treat @ x1 x2 @@;
>
> But I would like to know how to read it with R, to get rid of my addiction
> to SAS.
>
> Thank You,
>
> Rock Ouimet
> DRF-MRNF, Quebec
> --
> View this message in context: http://n4.nabble.com/Read-data-in-sequences-tp1819487p1819487.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.
>



More information about the R-help mailing list