SlideShare a Scribd company logo
3
Most read
12
Most read
19
Most read
FAO- Global Soil
Partnership
Training on
Digital Soil Organic Carbon
Mapping
20-24 January 2018
Tehran/Iran
Yusuf YIGINI, PhD - FAO, Land and Water Division (CBL)
Guillermo Federico Olmedo, PhD - FAO, Land and Water Division (CBL)
R - Import , Export
R- Import Data
The easiest way to create a data frame is to read in
data from a file—this is done using the function
read.table, which works with ASCII text files. Data
can be read in from other files as well, using
different functions, but read.table is the most
commonly used approach. R is very flexible in how
it reads in data from text files.
read.csv and read.csv2 are identical to read.table except for
the defaults. They are intended for reading ‘comma separated
value’ files (‘.csv’) or (read.csv2) the variant used in countries
that use a comma as decimal point and a semicolon as field
separator. Similarly, read.delim and read.delim2 are for
reading delimited files, defaulting to the TAB character for the
delimiter.
In various countries, as the comma “,” character
serves as the decimal point, the function
read.csv2 should be used instead!
R- Import Data
read.table("MASIS_SOC.csv", sep = ",")
read.csv("MASIS_SOC.csv", sep = ",")
read.csv2("MASIS_SOC.csv")
read.delim("MASIS_SOC.csv")
read.delim("MASIS_SOC.csv", sep = ",")
R- Import Data
read.table("MASIS_SOC.csv", sep = ",")
read.csv("MASIS_SOC.csv", sep = ",")
read.csv2("MASIS_SOC.csv")
read.delim("MASIS_SOC.csv")
read.delim("MASIS_SOC.csv", sep = ",")
R- Import Data
Unless you take any special action, read.table() reads all the
columns as character vectors and then tries to select a
suitable class for each variable in the data frame. It tries in
logical, integer, numeric and complex. If all of these fail, the
variable is converted to a factor.
More about Factors: https://www.stat.berkeley.edu/classes/s133/factors.html
R- Import Data
> is.na(SOC)
Id UpperDepth LowerDepth SOC Lambda tsme Region
[1,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[3,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[6,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[9,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[10,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
...
R- Import Data
> anyNA(SOC)
[1] TRUE
> sum(is.na(SOC$SOC))
[1] 1
R- Import Data
Importing from other statistical systems
install.packages("foreign")
library(foreign)
stata <- read.dta(“data.dta”)
spss <- read.spss(“data.sav”, to.data.frame=TRUE)
sasxport <- read.xport(“sata.xpt”)
epiinfo <- read.epiinfo(“data.rec”) …
Note: The foreign package is in the standard distribution. It
handles import and export of data.
R- Import Data
R.matlab: Read and Write MAT Files and Call MATLAB from
Within R
Methods readMat() and writeMat() for reading and writing MAT
files. For user with MATLAB v6 or newer installed (either locally
or on a remote host), the package also provides methods for
controlling MATLAB via R and sending and retrieving data
between R and MATLAB.
R- Import Data
Reading Data from Web
> read.table("http://www.cdc.noaa.gov/data/correlation/nao.data",skip=1,
nrow=70, na.strings="-99.90")
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13
1 1948 NA NA NA NA NA NA NA NA NA NA NA NA
2 1949 NA NA NA NA NA NA NA NA NA NA NA NA
3 1950 0.56 0.01 -0.78 0.65 -0.50 0.25 -1.23 -0.19 0.39 1.43 -1.46 -1.03
4 1951 -0.42 0.35 -1.47 -0.38 -0.50 -1.35 1.39 -0.41 -1.18 2.54 -0.54 1.13
5 1952 0.57 -1.38 -1.97 0.95 -0.99 -0.10 -0.06 -0.49 -0.38 -0.28 -1.32 -0.49
6 1953 -0.12 -1.00 -0.45 -1.96 -0.56 1.41 0.43 -1.04 -0.19 1.95 0.96 -0.52
7 1954 -0.08 0.40 -1.27 1.31 -0.03 0.06 -0.57 -2.57 -0.28 1.16 0.29 0.55
8 1955 -2.65 -1.71 -0.96 -0.60 -0.26 -0.80 1.78 1.25 0.46 -1.09 -1.49 0.07
9 1956 -0.76 -1.71 -0.46 -1.30 2.10 0.41 -0.72 -1.89 0.38 1.47 0.40 0.00
R- Import Data
Reading Data from Web
> read.table("http://www.cdc.noaa.gov/data/correlation/nao.data",skip=1,
nrow=70)
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13
1 1948 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90
2 1949 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90
3 1950 0.56 0.01 -0.78 0.65 -0.50 0.25 -1.23 -0.19 0.39 1.43 -1.46 -1.03
4 1951 -0.42 0.35 -1.47 -0.38 -0.50 -1.35 1.39 -0.41 -1.18 2.54 -0.54 1.13
5 1952 0.57 -1.38 -1.97 0.95 -0.99 -0.10 -0.06 -0.49 -0.38 -0.28 -1.32 -0.49
6 1953 -0.12 -1.00 -0.45 -1.96 -0.56 1.41 0.43 -1.04 -0.19 1.95 0.96 -0.52
7 1954 -0.08 0.40 -1.27 1.31 -0.03 0.06 -0.57 -2.57 -0.28 1.16 0.29 0.55
8 1955 -2.65 -1.71 -0.96 -0.60 -0.26 -0.80 1.78 1.25 0.46 -1.09 -1.49 0.07
9 1956 -0.76 -1.71 -0.46 -1.30 2.10 0.41 -0.72 -1.89 0.38 1.47 0.40 0.00
10 1957 0.71 -0.32 -1.73 0.39 -0.68 -0.42 -1.16 -0.83 -1.47 1.95 0.63 0.02
R- Import Data
Reading Data from Web
> read.table("http://www.cdc.noaa.gov/data/correlation/nao.data")
Error in scan(file = file, what = what, sep = sep, quote = quote, dec =
dec, : line 1 did not have 13 elements
R- Import Data
PostGIS Spatial Databases
PostGIS is a spatial database extender for
PostgreSQL object-relational database. It adds
support for geographic objects allowing location
queries to be run in SQL.
R- Import Data
PostGIS Spatial Databases
PostGIS is a spatial database extender for
PostgreSQL object-relational database. It adds
support for geographic objects allowing location
queries to be run in SQL.
R- Import Data
To read data from PostgreSQL into R, postGIStools
provides the get_postgis_query function. Like the
dbGetQuery function in PostgreSQL, it requires a
connection object and a SQL statement, which in
this case must be a SELECT statement. In addition,
the user may identify a geometry and/or hstore field
by name.
R- Read-Import Data
library(RPostgreSQL)
library(postGIStools)
con <- dbConnect(PostgreSQL(), dbname = "gsp_db", user = "GSP",
host = "bla-bla.com",
password = "587vn34m98dhu")
countries <- get_postgis_query(con, "SELECT * FROM country
WHERE SOCStck> 102",
geom_name = "geom", hstore_name =
"translations")
R- Save Data
> write.csv(SOC,file = "SOCData.csv")
The easiest way to do this is to use write.csv(). By default, write.
csv() includes row names, but these are usually unnecessary and
may cause confusion.
R- Save Data
> write.csv(SOC,file = "SOCData.csv")
The easiest way to do this is to use write.csv(). By default, write.
csv() includes row names, but these are usually unnecessary and
may cause confusion.
Saving in R data format
# Save in a text format that can be easily loaded in R
> dump("data", "data.Rdmpd")
# Can save multiple objects:
> dump(c("data", "data1"), "data.Rdmpd")
# To load the data again:
source("data.Rdmpd")
# When loaded, the original data names will automatically be used.
write.csv() and write.table() are best for interoperability with other
data analysis programs. They will not, however, preserve special
attributes of the data structures, such as whether a column is a
character type or factor, or the order of levels in factors. In order to
do that, it should be written out in a special format for R.
More on: Rdmpd: http://www.cookbook-r.com/Data_input_and_output/Writing_data_to_a_file/
Saving in R data format
# Save in a text format that can be easily loaded in R
> dump("data", "data.Rdmpd")
# Can save multiple objects:
> dump(c("data", "data1"), "data.Rdmpd")
# To load the data again:
source("data.Rdmpd")
# When loaded, the original data names will automatically be used.
write.csv() and write.table() are best for interoperability with other
data analysis programs. They will not, however, preserve special
attributes of the data structures, such as whether a column is a
character type or factor, or the order of levels in factors. In order to
do that, it should be written out in a special format for R.
More on: Rdmpd: http://www.cookbook-r.com/Data_input_and_output/Writing_data_to_a_file/

More Related Content

PPTX
Data Management in R
PPTX
Data visualization using R
PPTX
Exploratory data analysis in R - Data Science Club
PPT
14. Query Optimization in DBMS
PPTX
Text clustering
PDF
Object oriented databases
PDF
6. R data structures
PPT
Indexing and Hashing
Data Management in R
Data visualization using R
Exploratory data analysis in R - Data Science Club
14. Query Optimization in DBMS
Text clustering
Object oriented databases
6. R data structures
Indexing and Hashing

What's hot (20)

PPTX
Getting Started with R
PPT
Map reduce in BIG DATA
PPTX
Python Seaborn Data Visualization
PPT
Data preprocessing
PPTX
Chart and graphs in R programming language
PPTX
Introduction to pandas
PPTX
Data visualization with R
PDF
An Introduction to Data Mining with R
PPTX
Regular expressions in Python
PPTX
Data Structures in Python
PPTX
Multimedia Database
PPT
Python Pandas
PPTX
PDF
Data Visualization With R
PPTX
Major issues in data mining
PDF
Introduction to R Programming
PPT
C++ Data Structure PPT.ppt
PPTX
Step By Step Guide to Learn R
PPTX
Functions in python
PDF
K - Nearest neighbor ( KNN )
Getting Started with R
Map reduce in BIG DATA
Python Seaborn Data Visualization
Data preprocessing
Chart and graphs in R programming language
Introduction to pandas
Data visualization with R
An Introduction to Data Mining with R
Regular expressions in Python
Data Structures in Python
Multimedia Database
Python Pandas
Data Visualization With R
Major issues in data mining
Introduction to R Programming
C++ Data Structure PPT.ppt
Step By Step Guide to Learn R
Functions in python
K - Nearest neighbor ( KNN )
Ad

Similar to R data-import, data-export (20)

PDF
9. R data-import data-export
PDF
7. Data Import – Data Export
 
PDF
Data import-cheatsheet
PDF
@ R reference
PDF
R command cheatsheet.pdf
PDF
Reference card for R
PDF
Short Reference Card for R users.
PDF
R Programming Reference Card
PDF
R gráfico
PDF
20170509 rand db_lesugent
PDF
Data analystics with R module 3 cseds vtu
PPTX
R language introduction
PDF
tidyr.pdf
PPTX
Data Handling in R language basic concepts.pptx
PDF
20130215 Reading data into R
PPTX
R part I
PPTX
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
PDF
6. Vectors – Data Frames
 
PDF
R programming & Machine Learning
PDF
Introduction to r studio on aws 2020 05_06
9. R data-import data-export
7. Data Import – Data Export
 
Data import-cheatsheet
@ R reference
R command cheatsheet.pdf
Reference card for R
Short Reference Card for R users.
R Programming Reference Card
R gráfico
20170509 rand db_lesugent
Data analystics with R module 3 cseds vtu
R language introduction
tidyr.pdf
Data Handling in R language basic concepts.pptx
20130215 Reading data into R
R part I
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
6. Vectors – Data Frames
 
R programming & Machine Learning
Introduction to r studio on aws 2020 05_06
Ad

More from FAO (20)

PPTX
Nigeria
 
PPT
Niger
 
PPT
Namibia
 
PPT
Mozambique
 
PPT
Zimbabwe takesure
 
PPT
Zimbabwe
 
PPT
Zambia
 
PPT
Togo
 
PPT
Tanzania
 
PPT
Spal presentation
 
PPT
Rwanda
 
PPT
Nigeria uponi
 
PPTX
The multi-faced role of soil in the NENA regions (part 2)
 
PPTX
The multi-faced role of soil in the NENA regions (part 1)
 
PDF
Agenda of the launch of the soil policy brief at the Land&Water Days
 
PDF
Agenda of the 5th NENA Soil Partnership meeting
 
PPTX
The Voluntary Guidelines for Sustainable Soil Management
 
PPTX
GLOSOLAN - Mission, status and way forward
 
PPTX
Towards a Global Soil Information System (GLOSIS)
 
PPTX
GSP developments of regional interest in 2019
 
Nigeria
 
Niger
 
Namibia
 
Mozambique
 
Zimbabwe takesure
 
Zimbabwe
 
Zambia
 
Togo
 
Tanzania
 
Spal presentation
 
Rwanda
 
Nigeria uponi
 
The multi-faced role of soil in the NENA regions (part 2)
 
The multi-faced role of soil in the NENA regions (part 1)
 
Agenda of the launch of the soil policy brief at the Land&Water Days
 
Agenda of the 5th NENA Soil Partnership meeting
 
The Voluntary Guidelines for Sustainable Soil Management
 
GLOSOLAN - Mission, status and way forward
 
Towards a Global Soil Information System (GLOSIS)
 
GSP developments of regional interest in 2019
 

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Cell Types and Its function , kingdom of life
PDF
Computing-Curriculum for Schools in Ghana
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
master seminar digital applications in india
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
GDM (1) (1).pptx small presentation for students
FourierSeries-QuestionsWithAnswers(Part-A).pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Cell Types and Its function , kingdom of life
Computing-Curriculum for Schools in Ghana
Module 4: Burden of Disease Tutorial Slides S2 2025
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
master seminar digital applications in india
Microbial disease of the cardiovascular and lymphatic systems
Supply Chain Operations Speaking Notes -ICLT Program
202450812 BayCHI UCSC-SV 20250812 v17.pptx
A systematic review of self-coping strategies used by university students to ...
O5-L3 Freight Transport Ops (International) V1.pdf

R data-import, data-export

  • 1. FAO- Global Soil Partnership Training on Digital Soil Organic Carbon Mapping 20-24 January 2018 Tehran/Iran Yusuf YIGINI, PhD - FAO, Land and Water Division (CBL) Guillermo Federico Olmedo, PhD - FAO, Land and Water Division (CBL)
  • 2. R - Import , Export
  • 3. R- Import Data The easiest way to create a data frame is to read in data from a file—this is done using the function read.table, which works with ASCII text files. Data can be read in from other files as well, using different functions, but read.table is the most commonly used approach. R is very flexible in how it reads in data from text files.
  • 4. read.csv and read.csv2 are identical to read.table except for the defaults. They are intended for reading ‘comma separated value’ files (‘.csv’) or (read.csv2) the variant used in countries that use a comma as decimal point and a semicolon as field separator. Similarly, read.delim and read.delim2 are for reading delimited files, defaulting to the TAB character for the delimiter. In various countries, as the comma “,” character serves as the decimal point, the function read.csv2 should be used instead!
  • 5. R- Import Data read.table("MASIS_SOC.csv", sep = ",") read.csv("MASIS_SOC.csv", sep = ",") read.csv2("MASIS_SOC.csv") read.delim("MASIS_SOC.csv") read.delim("MASIS_SOC.csv", sep = ",")
  • 6. R- Import Data read.table("MASIS_SOC.csv", sep = ",") read.csv("MASIS_SOC.csv", sep = ",") read.csv2("MASIS_SOC.csv") read.delim("MASIS_SOC.csv") read.delim("MASIS_SOC.csv", sep = ",")
  • 7. R- Import Data Unless you take any special action, read.table() reads all the columns as character vectors and then tries to select a suitable class for each variable in the data frame. It tries in logical, integer, numeric and complex. If all of these fail, the variable is converted to a factor. More about Factors: https://www.stat.berkeley.edu/classes/s133/factors.html
  • 8. R- Import Data > is.na(SOC) Id UpperDepth LowerDepth SOC Lambda tsme Region [1,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE [2,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE [3,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE [5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE [6,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE [7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE [8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE [9,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE [10,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE ...
  • 9. R- Import Data > anyNA(SOC) [1] TRUE > sum(is.na(SOC$SOC)) [1] 1
  • 10. R- Import Data Importing from other statistical systems install.packages("foreign") library(foreign) stata <- read.dta(“data.dta”) spss <- read.spss(“data.sav”, to.data.frame=TRUE) sasxport <- read.xport(“sata.xpt”) epiinfo <- read.epiinfo(“data.rec”) … Note: The foreign package is in the standard distribution. It handles import and export of data.
  • 11. R- Import Data R.matlab: Read and Write MAT Files and Call MATLAB from Within R Methods readMat() and writeMat() for reading and writing MAT files. For user with MATLAB v6 or newer installed (either locally or on a remote host), the package also provides methods for controlling MATLAB via R and sending and retrieving data between R and MATLAB.
  • 12. R- Import Data Reading Data from Web > read.table("http://www.cdc.noaa.gov/data/correlation/nao.data",skip=1, nrow=70, na.strings="-99.90") V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 1 1948 NA NA NA NA NA NA NA NA NA NA NA NA 2 1949 NA NA NA NA NA NA NA NA NA NA NA NA 3 1950 0.56 0.01 -0.78 0.65 -0.50 0.25 -1.23 -0.19 0.39 1.43 -1.46 -1.03 4 1951 -0.42 0.35 -1.47 -0.38 -0.50 -1.35 1.39 -0.41 -1.18 2.54 -0.54 1.13 5 1952 0.57 -1.38 -1.97 0.95 -0.99 -0.10 -0.06 -0.49 -0.38 -0.28 -1.32 -0.49 6 1953 -0.12 -1.00 -0.45 -1.96 -0.56 1.41 0.43 -1.04 -0.19 1.95 0.96 -0.52 7 1954 -0.08 0.40 -1.27 1.31 -0.03 0.06 -0.57 -2.57 -0.28 1.16 0.29 0.55 8 1955 -2.65 -1.71 -0.96 -0.60 -0.26 -0.80 1.78 1.25 0.46 -1.09 -1.49 0.07 9 1956 -0.76 -1.71 -0.46 -1.30 2.10 0.41 -0.72 -1.89 0.38 1.47 0.40 0.00
  • 13. R- Import Data Reading Data from Web > read.table("http://www.cdc.noaa.gov/data/correlation/nao.data",skip=1, nrow=70) V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 1 1948 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 2 1949 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 -99.90 3 1950 0.56 0.01 -0.78 0.65 -0.50 0.25 -1.23 -0.19 0.39 1.43 -1.46 -1.03 4 1951 -0.42 0.35 -1.47 -0.38 -0.50 -1.35 1.39 -0.41 -1.18 2.54 -0.54 1.13 5 1952 0.57 -1.38 -1.97 0.95 -0.99 -0.10 -0.06 -0.49 -0.38 -0.28 -1.32 -0.49 6 1953 -0.12 -1.00 -0.45 -1.96 -0.56 1.41 0.43 -1.04 -0.19 1.95 0.96 -0.52 7 1954 -0.08 0.40 -1.27 1.31 -0.03 0.06 -0.57 -2.57 -0.28 1.16 0.29 0.55 8 1955 -2.65 -1.71 -0.96 -0.60 -0.26 -0.80 1.78 1.25 0.46 -1.09 -1.49 0.07 9 1956 -0.76 -1.71 -0.46 -1.30 2.10 0.41 -0.72 -1.89 0.38 1.47 0.40 0.00 10 1957 0.71 -0.32 -1.73 0.39 -0.68 -0.42 -1.16 -0.83 -1.47 1.95 0.63 0.02
  • 14. R- Import Data Reading Data from Web > read.table("http://www.cdc.noaa.gov/data/correlation/nao.data") Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 1 did not have 13 elements
  • 15. R- Import Data PostGIS Spatial Databases PostGIS is a spatial database extender for PostgreSQL object-relational database. It adds support for geographic objects allowing location queries to be run in SQL.
  • 16. R- Import Data PostGIS Spatial Databases PostGIS is a spatial database extender for PostgreSQL object-relational database. It adds support for geographic objects allowing location queries to be run in SQL.
  • 17. R- Import Data To read data from PostgreSQL into R, postGIStools provides the get_postgis_query function. Like the dbGetQuery function in PostgreSQL, it requires a connection object and a SQL statement, which in this case must be a SELECT statement. In addition, the user may identify a geometry and/or hstore field by name.
  • 18. R- Read-Import Data library(RPostgreSQL) library(postGIStools) con <- dbConnect(PostgreSQL(), dbname = "gsp_db", user = "GSP", host = "bla-bla.com", password = "587vn34m98dhu") countries <- get_postgis_query(con, "SELECT * FROM country WHERE SOCStck> 102", geom_name = "geom", hstore_name = "translations")
  • 19. R- Save Data > write.csv(SOC,file = "SOCData.csv") The easiest way to do this is to use write.csv(). By default, write. csv() includes row names, but these are usually unnecessary and may cause confusion.
  • 20. R- Save Data > write.csv(SOC,file = "SOCData.csv") The easiest way to do this is to use write.csv(). By default, write. csv() includes row names, but these are usually unnecessary and may cause confusion.
  • 21. Saving in R data format # Save in a text format that can be easily loaded in R > dump("data", "data.Rdmpd") # Can save multiple objects: > dump(c("data", "data1"), "data.Rdmpd") # To load the data again: source("data.Rdmpd") # When loaded, the original data names will automatically be used. write.csv() and write.table() are best for interoperability with other data analysis programs. They will not, however, preserve special attributes of the data structures, such as whether a column is a character type or factor, or the order of levels in factors. In order to do that, it should be written out in a special format for R. More on: Rdmpd: http://www.cookbook-r.com/Data_input_and_output/Writing_data_to_a_file/
  • 22. Saving in R data format # Save in a text format that can be easily loaded in R > dump("data", "data.Rdmpd") # Can save multiple objects: > dump(c("data", "data1"), "data.Rdmpd") # To load the data again: source("data.Rdmpd") # When loaded, the original data names will automatically be used. write.csv() and write.table() are best for interoperability with other data analysis programs. They will not, however, preserve special attributes of the data structures, such as whether a column is a character type or factor, or the order of levels in factors. In order to do that, it should be written out in a special format for R. More on: Rdmpd: http://www.cookbook-r.com/Data_input_and_output/Writing_data_to_a_file/