R programming: The Simplest, Universal Way for Importing Dataset by RStudio

AgriEnvCoder
4 min readJun 11, 2020

--

Since you have started learning R, you probably learned how to insert data using one line code like this:

Dataset <- read.csv("/Users/supermonk00/Desktop/programing/R/Curfew-project/Covid-19 Bar chart race in Carib/record.2020.04.01.csv")

or some tutorials refer to insert the data only if you set in the right the working directory. For example:

setwd("/Users/supermonk00/Desktop/programing/R/Curfew-project/Covid-19 Bar chart race in Carib")
Dataset <- read.csv("record.2020.04.01.csv")

However, there are too much different types of code for inserting that is hard to grasp, such as read.table(), delim(), read_excel()….. and so on.

Depending on the type of dataset, sometime you would have to library()a certain package. Moreover, some datasets need to note (header = FALSE), (sep = ",")or (skip = 1). It is quite difficult for R beginners to adjust code several times for a prefect data frame insertion. Nevertheless, using the convenient function in RStudio interface, you can insert a dataset simply and flexibly.

The following steps would introduce you the simplest and universal way for importing dataset by R Studio”

(1) Open RStudio

Note: the version might not influence much, so don’t worry you possess different version from mine(my version is 1.1.456) .

(2) Switch the interface to [Environment], and click [Import Dataset]

(3) Select the menu fitted the file type

Based on the file type that you want to insert, you can choose the options in menu for inserting.

For example:

.txt is [From text (base)]

.xlsx is [From Excel]

Generally the file type would be .csv ,.txt or.xlsx. Of course, it also can import dataset from SPSS, SAS or the other statistical softwares.

Next, I would use file type:.dat for instance.

(4) Search and open the target data

Scroll down the menu and click [From text (reader)], and the interface would become like this picture below.

Then, click [Browse] to search the target file.

After finding the file, open it.

(5) Modify the data frame

At this interactive interface, most commands could be added here. (col_types=...), (skip = ...) ,(header = FALSE) ,(sep = ",") as mentioning before could all be marked. Every change could be seen at [Code Preview].

(6) Copy and paste to R script

At this stage, click the right top copy button or highlight all the code in [Code Preview], use Ctrl+C (command+C) to copy, and paste it to the R script.

Run the code, and the data would be inserted as “Dataset” in the R environment.

Until now, the tutorial is done.

The advantage of this method not only can insert the data easily and flexibly but also the code in R script can be further modified, combined with the other code. Which let the data insert process become more automatic. For instance, it could be written in a function like this:

So that, it could be applied for inserting data frame in the same pattern at one time. Furthermore, if the other data frame is just slightly different, you can directly revise the code in the script.

To sum up, rather than writing code for importing dataset in R, R Studio provides the R user with a user-oriented design method of data insertion. Since that, you don’t have to worry about remembering every code for inserting different kinds of data!

--

--