[SOLVED]How Load CSV file into RGeos ??

If you have problems using RGeostats package or if you simply need some help on an unlisted subject.

[SOLVED]How Load CSV file into RGeos ??

Postby wassim » Wed Jun 04, 2014 3:12 pm

Hi,

can anyone please tell me how to load a csv file into the RGeos library ?? thx
wassim
 
Posts: 1
Joined: Wed Jun 04, 2014 2:59 pm

Re: How Load CSV file into RGeos ??

Postby Didier Renard » Fri Jun 06, 2014 8:06 pm

To read a CSV file, you must first load the contents of the file into a data.table. This can be realized using read.table() or read.csv() (standard functions of R language). This function allows you to specify the character used for separation, for decimal point, and for way to specify the missing value.

For example, if one considers the foolowing ASCII file (called "a.dat"):

1.2,2.4,3.2
2.4,4.2,2.2
1.1,Noval,3.
0.,0.,1.1

We just have to type:

dt = read.csv("a.dat",header=FALSE,na.strings="Noval")

This command creates the data.table called "dt" which is printed as follows:

> dt
V1 V2 V3
1 1.2 2.4 3.2
2 2.4 4.2 2.2
3 1.1 NA 3.0
4 0.0 0.0 1.1

We can check that the names of the fields (or columns) are created automatically. Knowing that, it can be clever to provide names already in the ASCII file as follows:

x1,x2,z1
1.2,2.4,3.2
2.4,4.2,2.2
1.1,Noval,3.
0.,0.,1.1

Then the following command:
dt = read.csv("a.dat",header=TRUE,na.strings="Noval")

leads to the following contents:
> dt
x1 x2 z1
1 1.2 2.4 3.2
2 2.4 4.2 2.2
3 1.1 NA 3.0
4 0.0 0.0 1.1

Ultimately, it suffices to load this data table to the specific command called db.create() in order to create a Db. Try:
> db = db.create(dt)

You have created a Db (for isolated samples), as demonstrated next:
> db

Data Base Characteristics
=========================

Data Base Summary
-----------------
File is organized as a set of isolated points
Space dimension = 2
Number of fields = 4
Maximum Number of attributes = 4
Total number of samples = 4

Variables
---------
Field = 1 - Name = rank - Locator = rank
Field = 2 - Name = x1 - Locator = x1
Field = 3 - Name = x2 - Locator = x2
Field = 4 - Name = z1 - Locator = z1
Didier Renard
 
Posts: 252
Joined: Thu Sep 20, 2012 4:22 pm


Return to Other Troubleshooting

Who is online

Users browsing this forum: No registered users and 1 guest

cron