about reading data with special rows

4 views
Skip to first unread message

lily li

unread,
Sep 28, 2015, 12:02:04 PM9/28/15
to ggp...@googlegroups.com
Hi ggplot users,

I want to import a dataset into R and draw figures using ggplot2. However, I met difficulties in reading the data first. For example, many rows have special characters which specify the statistics of the dataset. How to get rid of these rows beginning with //... and read the data finally? Thanks.

This code does not work:
data1 = read.table ('df.txt', comment.char='//', header=F)

df
//local time=2:30:20
//max number=10
1.0   5.6   7.8   1.2   1.6   2.0
1.2   6.6   7.6   1.0   1.5   3.4
1.3   6.5   7.6   1.4   1.5   3.5
//local time=2:30:30
//max number=20
1.4   5.6   6.7   1.0   1.5   2.1
1.3   5.5   6.6   1.2   1.4   2.2
1.2   5.4   6.6   1.2   1.5   2.3
...




Ben Bolker

unread,
Sep 28, 2015, 12:03:46 PM9/28/15
to lily li, ggp...@googlegroups.com
Really a generic R question rather than a ggplot question, but have
you tried comment.char="/"?
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

lily li

unread,
Sep 28, 2015, 12:20:27 PM9/28/15
to ggp...@googlegroups.com
Could anyone help me? Thanks very much.

Ben Bolker

unread,
Sep 28, 2015, 12:24:48 PM9/28/15
to ggp...@googlegroups.com
With exactly this data file:

//local time=2:30:20
//max number=10
1.0 5.6 7.8 1.2 1.6 2.0
1.2 6.6 7.6 1.0 1.5 3.4
1.3 6.5 7.6 1.4 1.5 3.5
//local time=2:30:30
//max number=20
1.4 5.6 6.7 1.0 1.5 2.1
1.3 5.5 6.6 1.2 1.4 2.2
1.2 5.4 6.6 1.2 1.5 2.3

This works for me:

read.table("slashdf.txt",comment.char="/")

V1 V2 V3 V4 V5 V6
1 1.0 5.6 7.8 1.2 1.6 2.0
2 1.2 6.6 7.6 1.0 1.5 3.4
3 1.3 6.5 7.6 1.4 1.5 3.5
4 1.4 5.6 6.7 1.0 1.5 2.1
5 1.3 5.5 6.6 1.2 1.4 2.2
6 1.2 5.4 6.6 1.2 1.5 2.3
Reply all
Reply to author
Forward
0 new messages