Importing Data Into R from a CSV File and creating a Chart out of It

Krishna Kanth
Beginner @ Data Science
3 min readOct 22, 2014

--

A basic data import operation into R environment!

R is a very powerful statistical tool for analytics. Most important of the operations that R performs revolves around data. So, today let us take a very brief look at how to import data into R from a CSV file.

Open your R console and get ready to drag some data!

First off, create an Excel file and enter some very simple data, something like:

Save the excel file as a .CSV file with a relevant name. I called it ‘InputDataCSV.csv’.

A CSV file is a ‘comma separated values’ file that delimits each data value with a ‘,’ or a ‘;’

To see your saved data in this format, open Notepad and drag-drop the CSV file you just created, and you would be seeing something like:

Now, our data is ready in required format. So, let’s get down to business and import the data in R right away.

Firstly, just check your current path in R. To do so, type getwd() and hit enter.

Put the CSV file you created in that directory.

Now let us create a variable ‘data’ (you can name it anything, I chose ‘data’), and store the file in that variable.
<variablename> <- read.csv(“<filename.csv>”)

data <- read.csv(“InputDataCSV.csv”)

Once you type that and hit enter, the variable stores the data present in the input csv file. To see what data that is, just type the name of the variable and hit enter, and the data would be displayed.

So that’s it. You’ve just imported data from an external file into your R environment.

Creating a basic chart from the imported data

Now let us make a very basic graph with the given data.

For that, all you have to do is use the ‘barplot()’ function on the imported data.

Type: barplot(height = data$Marks, names.arg = data$Names)

And there is your graph in the Plots area in your RStudio IDE.

That’s about importing basic data and plotting a basic graph.

Soon we shall see about creating other visually appealing graphs with advanced data, and thus witness why R is a grand statistical and analytical programming language for Data Scientists!

--

--