Read and import different data files in R: Excel, CSV, SPSS, txt, url
Steps to Read and import different data files in R
Using point and click option without R codes
1. Click on file (Top left corner)
2. Click on Import datasets
3. If you are importing,
• .txt file in R, then click on “from Text(base)”
• .csv file (comma separated values) in R, then click on “from Text(readr)”
• Excel file in R, then click on “Excel”
• SPSS file in R, then click on “SPSS
• SAS file in R, then click on “SAS”
4. This will open up prompt window, R will require an additional package names “readr” for importing this data set. R will ask “Do you want to install these packages”.
5. Click “yes”
6. Click on “Browse” (Top right)
7. Identify the location of the file which you would like to import
8. As soon as you identify the location, this will give you a data preview.
9. If the data preview is correct, then click on “Import”
Steps to read and import data with R codes
1. Reading data from .csv file (comma separated values)
Install the “readr” package
install.packages("readr")
library(readr)
Titanic <- read_csv("D:/2.0 Titanic/my_solution_tree.csv")
2. Reading data from Excel file
install.packages("readxl")
Recall readxl package for reading excel files
library(readxl)
store_visits <- read_excel("D:/store visits.xlsx")
View(store_visits)
3. Reading data from Text file
install.packages("read.table")
library("read.table")
text <- read.table("C:/Users/Ateeque Shaikh/Desktop/text.txt", header=TRUE, quote="\"")
Where,
text – name of the file
read.table – command for reading the text file
“” – within double quotes is the path where your is located
Header = TRUE – takes the header as the name
Quote = “” – considers the data in double quotes
View(text)
4. Reading data from SPSS file
# Recall library haven – for import and export of SPSS Stata and SAS files
install.packages("haven")
library(haven)
Train <- read_sav("D:/Train.sav")
View(text)
5. Reading data from from a weblink or url
# https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data
iris <- read.csv(url("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"), header = FALSE)
# Assigning names to the columns/ variables in the url
names(iris) <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
View(iris)