[R] Read text file

David Carlson dcarlson at tamu.edu
Tue Mar 11 15:19:10 CET 2014


If you read the manual page for read.csv (?read.csv), you will
see that the default for the header argument is TRUE, but your
example has no header. You do not need sep="," because that is
the default for read.csv. Also, the default is to convert
character strings to factors which you probably do not want
either. So you need

dta <- read.csv("sensor_0.log", header=FALSE,
stringsAsFactors=FALSE)

But you still may have problems. Several variables have a
mixture of numbers and characters. Use str(dta) to see this so
the numbers will be treated as character strings. Also the first
variable (V1) consists of the first 27 characters since spaces
are not separators:

dta$V1
> dta$V1
[1] "1377262633.948000    $GPRMC" "1377262633.958000    $GPVTG"
[3] "1377262633.968000    $GPGGA" "1377262633.978000    $GPGSA"
[5] "1377262633.998000    $GPGSV" "1377262634.008000    $GPGSV"
[7] "1377262634.028000    $GPGSV"

To split those into separate variables use

> dta$V1a <- substr(dta$V1, 1, 17)
> dta$V1b <- substr(dta$V1, 22, 27)

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Pascal Oettli
Sent: Tuesday, March 11, 2014 7:08 AM
To: Alaios
Cc: R. Help
Subject: Re: [R] Read text file

Hello,

See the "fill" option of the "read.csv" function. But be
careful, it
might lead to erroneous results, as explained in the help
page...

And there is neither $GPGLL nor $GPGLA in your example.

Regards,
Pascal

On Tue, Mar 11, 2014 at 9:00 PM, Alaios <alaios at yahoo.com>
wrote:
> Hi all
> I am trying to read some text files with the following format:
>
> 1377262633.948000
$GPRMC,125708.00,A,5047.66107,N,00603.65528,E,0.203,247.36,23081
3,,,A*60
> 1377262633.958000    $GPVTG,247.36,T,,M,0.203,N,0.377,K,A*3B
> 1377262633.968000
$GPGGA,125708.00,5047.66107,N,00603.65528,E,1,09,0.85,169.3,M,46
.5,M,,*52
> 1377262633.978000
$GPGSA,A,3,29,21,31,25,16,05,06,13,27,,,,1.78,0.85,1.57*0C
> 1377262633.998000
$GPGSV,3,1,12,03,01,266,,05,16,043,39,06,21,263,43,13,07,330,43*
70
> 1377262634.008000
$GPGSV,3,2,12,16,37,302,45,18,03,149,,21,59,166,33,23,04,304,16*
75
> 1377262634.028000
$GPGSV,3,3,12,25,18,129,21,27,11,260,39,29,45,071,47,31,35,211,4
7*7C
>
> but this returns me the following:
>
> read.csv("sensor_0.log",sep=",")
> Error in read.table(file = file, header = header, sep = sep,
quote = quote,  :
>   more columns than column names
>
> I guess the problem is that the columns are not consistent on
a per row basis.
> What  I am trying to do though is to read only the lines that
contain the $GPGLL or the $GPGLA entries (in the example they
corresponds to 3rd and 4th line).
> How can I do this in R?
>
> Regards
> A
>         [[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.
>



-- 
Pascal Oettli
Project Scientist
JAMSTEC
Yokohama, Japan

______________________________________________
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