How can I read a space-delimited file, where the data values for each case are folded before column 80, and so appear on two lines for each case? The first few cases look like this loc type bio H2S sal Eh7 pH buf P K Ca Mg Na Mn Zn Cu NH4 OI DVEG 676 -610 33 -290 5.00 2.34 20.238 1441.67 2150.00 5169.05 35184.5 14.2857 16.4524 5.02381 59.524 OI DVEG 516 -570 35 -268 4.75 2.66 15.591 1299.19 1844.76 4358.03 28170.4 7.7285 13.9852 4.19019 51.378 OI DVEG 1052 -610 32 -282 4.20 4.18 18.716 1154.27 1750.36 4041.27 26455.0 17.8066 15.3276 4.79221 68.788 OI DVEG 868 -560 30 -232 4.40 3.60 22.821 1045.15 1674.36 3966.08 25072.9 49.1538 17.3128 4.09487 82.256 The complete data is at: http://www.math.yorku.ca/SCS/viscollin/data/linthall.dat thanks, -Michael -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html Toronto, ONT M3J 1P3 CANADA
Try this: lines <- read.table(" http://www.math.yorku.ca/SCS/viscollin/data/linthall.dat", fill = TRUE, header = TRUE) DF <- do.call(rbind, lapply(split(lines, rep(1:(nrow(lines)/2), each = 2)), function(x)cbind(x[1, 1:13], x[2, 1:4]))) names(DF) <- names(lines) On Mon, Sep 1, 2008 at 1:06 PM, Michael Friendly <friendly@yorku.ca> wrote:> How can I read a space-delimited file, where the data values for each case > are folded before column 80, and so appear on two lines for each case? > > The first few cases look like this > > loc type bio H2S sal Eh7 pH buf P K Ca Mg Na Mn Zn Cu NH4 > OI DVEG 676 -610 33 -290 5.00 2.34 20.238 1441.67 2150.00 5169.05 35184.5 > 14.2857 16.4524 5.02381 59.524 > OI DVEG 516 -570 35 -268 4.75 2.66 15.591 1299.19 1844.76 4358.03 28170.4 > 7.7285 13.9852 4.19019 51.378 > OI DVEG 1052 -610 32 -282 4.20 4.18 18.716 1154.27 1750.36 4041.27 26455.0 > 17.8066 15.3276 4.79221 68.788 > OI DVEG 868 -560 30 -232 4.40 3.60 22.821 1045.15 1674.36 3966.08 25072.9 > 49.1538 17.3128 4.09487 82.256 > > The complete data is at: > > http://www.math.yorku.ca/SCS/viscollin/data/linthall.dat > > thanks, > -Michael > > -- > Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology > Dept. > York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 > 4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html > Toronto, ONT M3J 1P3 CANADA > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Dear Michael: There are doubtless better ways to do this, but the I just got the following ugly hack to work for me: rawDat <- readLines('linthall.dat') (N <- length(rawDat)) N2 <- ((N-1)/2) outDat <- character(N2+1) outDat[1] <- rawDat[1] for(i in 1:N2) outDat[i+1] <- paste(rawDat[(2*i)+0:1], collapse=' ') outFile <- 'linthall2.dat' writeLines(outDat, outFile) linthall <- read.table(outFile, header=TRUE) str(linthall) Hope this helps. Spencer Michael Friendly wrote:> How can I read a space-delimited file, where the data values for each > case > are folded before column 80, and so appear on two lines for each case? > > The first few cases look like this > > loc type bio H2S sal Eh7 pH buf P K Ca Mg Na Mn Zn Cu NH4 > OI DVEG 676 -610 33 -290 5.00 2.34 20.238 1441.67 2150.00 5169.05 35184.5 > 14.2857 16.4524 5.02381 59.524 > OI DVEG 516 -570 35 -268 4.75 2.66 15.591 1299.19 1844.76 4358.03 > 28170.4 7.7285 13.9852 4.19019 51.378 > OI DVEG 1052 -610 32 -282 4.20 4.18 18.716 1154.27 1750.36 4041.27 > 26455.0 > 17.8066 15.3276 4.79221 68.788 > OI DVEG 868 -560 30 -232 4.40 3.60 22.821 1045.15 1674.36 3966.08 25072.9 > 49.1538 17.3128 4.09487 82.256 > > The complete data is at: > > http://www.math.yorku.ca/SCS/viscollin/data/linthall.dat > > thanks, > -Michael >
Michael Friendly wrote:> How can I read a space-delimited file, where the data values for each case > are folded before column 80, and so appear on two lines for each case? > > The first few cases look like this > > loc type bio H2S sal Eh7 pH buf P K Ca Mg Na Mn Zn Cu NH4 > OI DVEG 676 -610 33 -290 5.00 2.34 20.238 1441.67 2150.00 5169.05 35184.5 > 14.2857 16.4524 5.02381 59.524 > OI DVEG 516 -570 35 -268 4.75 2.66 15.591 1299.19 1844.76 4358.03 28170.4 > 7.7285 13.9852 4.19019 51.378 > OI DVEG 1052 -610 32 -282 4.20 4.18 18.716 1154.27 1750.36 4041.27 26455.0 > 17.8066 15.3276 4.79221 68.788 > OI DVEG 868 -560 30 -232 4.40 3.60 22.821 1045.15 1674.36 3966.08 25072.9 > 49.1538 17.3128 4.09487 82.256 > > The complete data is at: > > http://www.math.yorku.ca/SCS/viscollin/data/linthall.dat > >scan() has a multi.line argument, so this works: varnames <- read.table(url("http://www.math.yorku.ca/SCS/viscollin/data/linthall.dat"), nrows=1, as.is=T)[1,] what <- c(list("a", "a"), as.list(rep(1, length(varnames)-2))) names(what) <- varnames data <- as.data.frame(scan(url("http://www.math.yorku.ca/SCS/viscollin/data/linthall.dat"), skip=1, what=what)) Duncan Murdoch