The post is about data in R language. Learn how to save and read data in R with this comprehensive guide. Discover methods like write.csv()
, saveRDS()
, and read.table()
, understand keyboard input using readline()
, scan()
, and master file-based data loading for matrices and datasets. Perfect for beginners and intermediate R users!
Table of Contents
How can you Save the Data in R Language?
To save data in R Language, there are many ways. The easiest way of saving data in R is to click Data –> Active Data Set –> Export Active Data. A dialogue box will appear. Click OK in the dialogue box. The data will be saved. The other ways to save data in R are:
Saving to CSV Files
# Base R package write.csv(Your_DataFrae, "path/to/file.csv", row.names = FALSE) #readr (tidyverse) Package library(readr) write_csv(your_DataFrame, "path/to/file.csv")
Saving to MS Excel Files
To save data to Excel files, the writexl or openxlsx package can be used
library(writexl) write_xlsx(your_DataFrame, "path/to/file.xlsx")
Saving to R’s Native Formats
Single or Multiple objects can be saved to a single file, such as RData
# .RData file save(object1, object2, file = "path/to/data.RData") # .rds file saveRDS(your_DataFrame, "path/to/data.rds")
Saving to Text Files
Data can be saved to text files using the following commands:
# Using Base R Package write.table(your_DataFrame, "path/to/file.txt", sep = "\t", row.names = FALSE) # using readr Package write_delim(your_DataFrame, "path/to/file.txt", delim = "\t")
Saving to JSON File Format
The data can be saved to a JSON file format using the jsonlite package.
write_json(your_DataFrame, "path/to/file.json")
Saving Data to Databases
Write data to SQL databases (for example, SQLite, PostgreSQL), for example
library(DBI) library(RSQLIte) # Create a database connect con <- dbConnect(RSQLite::SQLite(), "path/to/database.db") # Write a data frame to the database dbWriteTable(con, "table_name", your_DataFrame) # Disconnect when done dbDisconnect(con)
Saving Data to Other Statistical Software Formats
The haven
package can be used to save data for SPSS, Stata, or SAS. For example
library(haven) write_sav(your_DataFrame, "path/to/file.sav") # SPSS file format write_dta(your_DataFrame, "path/to/file.dta") # STATA file format
It is important to note that
- File Paths: Use absolute file paths, for example, D:/projects/data.csv, or relative paths such as data/file.csv.
- Overwriting: By default, R will overwrite existing files. Add checks to avoid accidental loss, for example,
if (!file.exists("file.csv")){
write.csv(your_DataFrame, "file.csv")
}
How to Read Data from the Keyboard?
To read the data from the keyboard, one can use the following functions
- scan(): read data by directly pressing keyboard keys
- deadline(): read text lines from a file connection
- print(): used to display specified keystrokes on the display/monitor.
Explain How to Read Data or a Matrix from a File?
- read.table(): usually read.table() function is used to read data. The default value of a header is set to “FALSE,” and hence, when we do not have a header, we need not use this argument.
- Use read.csv() or read.table() function to import/read spreadsheet exported files in which columns are separated by commas instead of white spaces. For MS Excel file use, read.xls() function.
- When you read in a matrix using read.table(), the resultant object will become a data frame, even when all the entries got to be numeric. The as.matrix() function can be used to read it into a matrix form like this
as.matrix(x, nrow = 5, byrow=T)
What is scan() Function in R?
The scan() function is used to read data into a vector or list from the console or a file.
z <- scan() 1: 12 5 3: 5 4: Read 3 items z ### Output 12 5 5
What is readline() Function in R?
The readline() function is used to read text lines from a connection. The readline() function is used for inputting a line from the keyboard in the form of a string. For example,
w <- readline() xyz vw u w ## Output xyz vw u