This post covers how to import data from external sources in R Studio.
1. Excel file
1) Preparation
First, place an Excel file in the R project folder.
Also, readxl package should be installed.
install.packages("readxl")library(readxl)
2) Import
read_excel() function imports Excel files and at the same time, it can be designated as a data frame.
In the example below, the name of the Excel file is 'excel file name' and the name of the new data frame is 'df_ex'.
Make sure to type quotation marks ("") for the excel file name.
For the last step, check if the import was successful.
df_ex <- read_excel("excel file name.xlsx")
df_ex
3) Cautions
If the Excel file is not in the R project folder, you can designate the file directory.In this case, the quotation marks covers the whole directory, not just the file name.
df_ex <- read_excel("C:/Folder1/Folder2/excel file name.xlsx")
Also, read_excel() function recognizes the first row of the file as variable names.Therefore, if the first row of the Excel file is not variable names but data, col_names = F parameter should be used.
In this case, the variable names are automatically set as
df_ex <- read_excel("excel file name.xlsx", col_names = F)
It is also possible to choose specific sheet of an Excel file by using 'sheet' parameter.
df_ex <- read_excel("excel file name.xlsx", sheet = 4)
2. CSV file
1) Preparation
First, place a CSV file in the R project folder.
Unlike the case of Excel files, no additional package is required for importing CSV files.
2) Import
read.csv() function imports CSV files and simultaneously, it can be designated as a data frame.
In the example below, the name of the CSV file is 'csv file name' and the name of the new data frame is 'df_csv'.
Make sure to type quotation marks ("") for the file name.
For the last step, check if the import was successful.
df_csv <- read.csv("csv file name.csv")df_ex
3) Cautions
read.csv() function also recognizes the first row of the file as variable names.
In this case, header = F parameter should be used.
This does exactly the same job as 'col_names = F' parameter, but with different syntax.
df_csv <- read.csv("csv file name.csv", header = F)
When the data includes string variables, stringsAsFactors = F parameter should be used.
df_csv <- read.csv("csv file name.csv", stringAsFactors = F)
3. RDA file
1) Preparation
First, prepare an .rda or .rdata file and place it in the R project folder.
.rda or .rdata files are R exclusive data files which are a lot faster and lighter than other types of file.
2) Import
To import .rda files, we use load() function.
load("rda file name.rda")
rda file name
3) Cautions
Importing .rda files does not require designation, unlike the case of Excel or CSV file.
(Thumbnail image: photo by Mika Baumeister on Unsplash)
 
![[R] Data Import](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5c_zi7m6ac3-R1bYIJyT3W6OQT7hc_EM4VLG7AGHx4KTh34WM1TNnGdU5Ft1mTclDmN1_U91hHixDjM1FFnGa8bZYnpykFWAMGv_EBKXyrWWjrPcxOc25YzY50igIPuznUsEyWRlvjKJpbO6EOCAn3GPlHE3wg2QSOLiqbkLfUazVALtPSH93WE3CTw/w72-h72-p-k-no-nu/mika-baumeister-Wpnoqo2plFA-unsplash.jpg) 
0 Comments