How to Import Data in R: Quick Guide
In R language, data can be imported using the following methods:
- Import a text file using the read.table() function, for example:
data <- read.table("data.txt", header = TRUE)
- Importing a CSV file using the read.csv() function, for example:
data <- read.csv("data.csv")
- Before using the read.xlsx() function to import an Excel file, you need to first install the readxl package, for example:
install.packages("readxl")
library(readxl)
data <- read_excel("data.xlsx")
- Import R data files using the readRDS() function, for example:
data <- readRDS("data.rds")
- Functions can be used to import data in various specific data formats, for example, the fromJSON() function in the jsonlite package can be used for JSON files, and the read_xml() function in the XML2 package can be used for XML files.
After importing data, you can view basic information about the data using functions such as print() and summary(), or directly analyze and visualize the data.