Skip to content
Related Articles

Related Articles

Importing Data in R Script
  • Difficulty Level : Basic
  • Last Updated : 19 Jun, 2020

Importing .csv and .txt files in R Script can be achieved in many ways.

First, let’s consider a data-set which we can use for the demonstration. For this demonstration, we will use two examples of a single dataset, one in .csv form and another .txt

Reading a Comma-Separated Value(CSV) File

Method 1: Using read.csv() Function

The function has two parameters:

  • file.choose():
    It opens a menu to choose a csv file from the desktop.
  • header:
    It is to indicate whether the first row of the dataset is a variable name or not. Apply T/True if the variable name is present else put F/False.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

# import and store the dataset in data1
data1 <- read.csv(file.choose(), header=T)
  
# display the data
data1

chevron_right


Output:

Method 2: Using read.table() Function

This function specifies how the dataset is separated, in this case we take sep=”, “ as argument.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import and store the dataset in data2
data2 <- read.table(file.choose(), header=T, sep=", ")
  
# display data
data2

chevron_right


Output:

Reading a Tab-Delimited(txt) File

Method 1: Using read.delim() Function

The function has two parameters:

  • file.choose():
    It opens a menu to choose a csv file from the desktop.
  • header:
    It is to indicate whether the first row of the dataset is a variable name or not. Apply T/True if the variable name is present else put F/False.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import and store the dataset in data3
data3 <- read.delim(file.choose(), header=T)
  
# display the data
data3

chevron_right


Output:

Method 2: Using read.table() Function

This function specifies how the dataset is separated, in this case we take sep=”\t” as argument.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import and store the dataset in data4
data4 <- read.table(file.choose(), header=T, sep="\t")
  
# display the data
data4

chevron_right


Output:

Using R-Studio

Steps:

  • From the Environment Tab click on the Import Dataset Menu
  • Select the file extension from the option
  • In the third step, a pop-up box will appear, either enter the file name or browse the desktop.
  • The selected file will be displayed on a new window with its dimensions.
  • In order to see the output on the console, type the filename.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # display the dataset
    dataset

    chevron_right

    
    

    Output:

  • In order to load the data onto the console for use, we use the attach command.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # To load the data for use
    attach(dataset)

    chevron_right

    
    

My Personal Notes arrow_drop_up
Recommended Articles
Page :