Load Data
Load CSV Files in R
Code Breakdown:
read.csv
used to load csv files.header = TURE
argument will consider the first row as the header or column names.
Get Data from the URL
-> Option 1: Directly save as an object.
Let's say the data is in csv format. We can use read.csv
function to directly parse the data and save it as a DataFrame.
Code Breakdown:
We have to put the URL inside the double quote under
read.csv
function.na.strings = "--"
: This dataset denotes missing data as--
. But R doesn't understand that. So we converted the--
intoNA
.as.is = TRUE
: Normally R converts the character column into a factor. By this argument, we specified not to do this conversion.
-> Option 2: First save as a file and then load as an object.
Code Breakdown:
At first, the URL was specified by an object
url
.In the second code,
download.file
function downloaded the dataset.At 3rd code, we loaded the dataset as a csv file.
Load TSV Files in R
To load a TSV file in R, we can use either the read.delim()
function or the read_tsv()
function from the readr
package.
Using the read.delim()
function
read.delim()
functionThe read.delim()
function is a general function for reading delimited text files. To read a TSV file, you need to specify the delimiter as "\t"
.
Using the read_tsv()
function
read_tsv()
functionThe read_tsv()
function is a specific function for reading TSV files. It is more efficient than using the read.delim()
function for TSV files.
To use the read_tsv()
function, you need to install the readr
package first.
When you observe the outputs, the basic difference between both methods is read_tsv()
function returns the dataframe with columns by specifying the type of it [ Student_Id – double, Student_Name – Character ],
when it comes to read.delim()
method it simply returns the data present in the tsv file.
However, the read_tsv()
function is more efficient and easier to use for TSV files.
Load Excel Files in R
To load an .xlsx
file in R, you can use the read_excel()
function from the readxl
package.
Loading a specific sheet from an XLSX file
If you want to load a specific sheet from an XLSX file, you can use the sheet
argument to the read_excel()
function.
For example, to load the sheet named "Sheet1
" from the XLSX file data.xlsx
, you would use the following code:
Last updated