[R] Insert text in data.frame

jim holtman jholtman at gmail.com
Wed Jul 2 23:27:48 CEST 2008


Try this:

x <- read.table(textConnection("Conc             Lat       Lon    Depth

Point0000   56.25    -5.65      70

Point0001   56.55    -5.35      85

Point0002   56.25    -5.65      65

Point0003   56.37    -5.21      80

Point0004   56.45    -5.23      30

Point0000   56.25    -5.55      75

Point0001   56.35    -5.34      85

Point0002   56.28    -5.18      75

Point0003   56.25    -5.10      45

Point0000   56.55    -5.65      50"), header=TRUE)
closeAllConnections()
# get the text strings for output
y <- capture.output(write.table(x, quote=FALSE, row.names=FALSE,
col.names=FALSE))
output <- file('tempxx.txt', 'w')
for (i in y){
    if (length(grep("Point0000", i)) > 0)cat("\nObject=Line\nColor
RED\nStyle SOLID\n", file=output)
    cat(i, "\n", file=output)
}
close(output)


It produces and output file:


Object=Line
Color RED
Style SOLID
Point0000 56.25 -5.65 70
Point0001 56.55 -5.35 85
Point0002 56.25 -5.65 65
Point0003 56.37 -5.21 80
Point0004 56.45 -5.23 30

Object=Line
Color RED
Style SOLID
Point0000 56.25 -5.55 75
Point0001 56.35 -5.34 85
Point0002 56.28 -5.18 75
Point0003 56.25 -5.1 45

Object=Line
Color RED
Style SOLID
Point0000 56.55 -5.65 50


On Wed, Jul 2, 2008 at 10:39 AM, Mafalda Viana <M.Viana at marlab.ac.uk> wrote:
> Dear R-helper,
>
>
>
> I wonder if anyone can help me. I am trying to convert a dataset to a
> format recognizable by a software onboard a research vessel but I am
> having problems with some steps.
>
>
>
> I have a data frame as follows:
>
>
>
> Conc             Lat       Lon    Depth
>
> Point0000   56.25    -5.65      70
>
> Point0001   56.55    -5.35      85
>
> Point0002   56.25    -5.65      65
>
> Point0003   56.37    -5.21      80
>
> Point0004   56.45    -5.23      30
>
> Point0000   56.25    -5.55      75
>
> Point0001   56.35    -5.34      85
>
> Point0002   56.28    -5.18      75
>
> Point0003   56.25    -5.10      45
>
> Point0000   56.55    -5.65      50
>
> ...
>
>
>
> My question is: how can I insert an empty row and the text below just
> before every "Point0000" of the column Conc?
>
>
>
> Object=Line
>
> Color RED
>
> Style SOLID
>
>
>
>
>
> The result I expect is as follows:
>
>
>
> Object=Line
>
> Color RED
>
> Style SOLID
>
> Point0000   56.25    -5.65      70
>
> Point0001   56.55    -5.35      85
>
> Point0002   56.25    -5.65      65
>
> Point0003   56.37    -5.21      80
>
> Point0004   56.45    -5.23      30
>
>
>
> Object=Line
>
> Color RED
>
> Style SOLID
>
> Point0000   56.25    -5.55      75
>
> Point0001   56.35    -5.34      85
>
> Point0002   56.28    -5.18      75
>
> Point0003   56.25    -5.10      45
>
>
>
> Object=Line
>
> Color RED
>
> Style SOLID
>
> Point0000   56.55    -5.65      50
>
>
>
>
>
> Thank you very much for your help.
>
>
>
> Best regards,
>
> Mafalda
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
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