How can R read and import data files in different formats?
In R language, various functions can be used to read and import different types of data files. Here are some commonly used methods:
- Import data from a CSV file into R.
data <- read.csv("file.csv")
- xl read
- import the function read_excel()
library(readxl)
data <- read_excel("file.xlsx")
- Read lines.
data <- readLines("file.txt")
- originating from another country
- Open the SPSS file for reading.
library(foreign)
data <- read.spss("file.sav", to.data.frame=TRUE)
- Relational Database Connectivity using R.
- retrieve data using SQL is obtained through the use of the function sqlFetch()
library(RODBC)
conn <- odbcConnect("DSN")
data <- sqlFetch(conn, "table_name")
The above are some common methods, you can select the appropriate function to read and import data based on different data file formats.