Perform the following steps to read and write data with R:
- To view the built-in datasets of R, type the following command:
> data()
- R will return a list of datasets in a dataset package, and the list comprises the name and description of each dataset.
- To load the dataset iris into an R session, type the following command:
> data(iris)
- The dataset iris is now loaded into the DataFrame format, which is a common
data structure in R to store a data table.
- To view the data type of iris, simply use the class function:
> class(iris)
[1] "data.frame"
- The data.frame console print shows that the iris dataset is in the structure of DataFrame.
- Use the save function to store an object in a file. For example, to save the loaded iris data into myData.RData, use the following command:
> save(iris, file="myData.RData")
- Use the load function to read a saved object into an R session. For example, to load iris data from myData.RData, use the following command:
> load("myData.RData")
- In addition to using built-in datasets, R also provides a function to import data from text into a DataFrame. For example, the read.table function can format a given text into a DataFrame:
> test.data = read.table(header = TRUE, text = "
+ a b
+ 1 2
+ 3 4
+ ")
- You can also use row.names and col.names to specify the names of columns and rows:
> test.data = read.table(text = "
+ 1 2
+ 3 4",
+ col.names=c("a","b"),
+ row.names = c("first","second"))
- View the class of the test.data variable:
> class(test.data)
[1] "data.frame"
- The class function shows that the test.data variable contains a DataFrame.
- In addition to importing data by using the read.table function, you can use the write.table function to export data to a text file:
> write.table(test.data, file = "test.txt" , sep = " ")
- The write.table function will write the content of test.data into test.txt
(the written path can be found by typing getwd()), with a separation delimiter as white space.
- Similar to write.table, write.csv can also export data to a file. However, write.csv uses a comma as the default delimiter:
> write.csv(test.data, file = "test.csv")
- With the read.csv function, the csv file can be imported as a DataFrame. However, the last example writes column and row names of the DataFrame to the test.csv file. Therefore, specifying header to TRUE and row names as the first column within the function can ensure the read DataFrame will not treat the header and the first column as values:
> csv.data = read.csv("test.csv", header = TRUE, row.names=1)
> head(csv.data)
a b
1 1 2
2 3 4
This section will cover how to work with the database. To connect with PostgreSQL, the RPostgreSQL package is required which can be installed using this command:
> install.packages("RPostgreSQL")
It will install package in your system. You need to have active internet connection for this command to complete. Once installed you can use the package for accessing database. You need to have username, password, database name for accessing the PostgreSQL. Replace the value with your values for parameter in dbconnect function.
> require("RPostgreSQL")
> driver = dbDriver("PostgreSQL")
> connection = dbConnect(driver, dbname="restapp", host="localhost",
port=5432, user="postgres", password="postgres")
> dbExistsTable(connection, "country")
[1] TRUE
TRUE shows that table exists in the database. To query the table use.
> data = dbGetQuery(connection, "select * from country")
> class(data)
Output:
[1] "data.frame"
> data
Output:
id code name
1 1 US USA
2 43 AS Austria
3 55 BR Brazil
Reading table data will result in to DataFrame in R.