R Data Import: Read Any File Format
In the R programming language, various functions can be used to read and import data files in different formats, as shown below:
- – Use the function read.table()
- Import a CSV file into R.
data <- read.table("data.txt", header = TRUE)
# 或
data <- read.csv("data.csv")
- “view Excel sheets”
- import the data from an Excel file
- The term “readxl” can be interpreted as reading Excel files using R programming language.
install.packages("readxl")
library(readxl)
data <- read_excel("data.xlsx")
- Database Interface
- Database Interface
install.packages("DBI")
library(DBI)
con <- dbConnect(RSQLite::SQLite(), "database.db")
data <- dbGetQuery(con, "SELECT * FROM table")
- from another country
- open the SPSS file
- – from another country
install.packages("foreign")
library(foreign)
data <- read.spss("data.sav", to.data.frame = TRUE)
- jsonlite is a library for working with JSON data in R.
- convert JSON to an object
- jsonlite is a package in R used for working with JSON data.
install.packages("jsonlite")
library(jsonlite)
data <- fromJSON("data.json")
By using the above methods, it is easy to read and import various types of data files into R for analysis and processing.