[R] Insert text in data.frame

0 views
Skip to first unread message

Mafalda Viana

unread,
Jul 2, 2008, 10:39:38 AM7/2/08
to r-h...@r-project.org
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-h...@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

unread,
Jul 2, 2008, 5:27:48 PM7/2/08
to Mafalda Viana, r-h...@r-project.org
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

--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

Reply all
Reply to author
Forward
0 new messages