This might me the first thing coming in to your mind, when you start learning R. Actually R can read most of datasets which are commonly used. Here in this post I am going to explain how you could import a spreadsheet to R’s global environment. After your installation of R and R studio (It is an easy setup, I don’t want to explain here), you need a basic idea on the R studio basic structure.

rconsole.jpg

Get the data from here. This dataset is an inbuilt database in R repository.You can access simply by typing “US Arrests” for this data on the console. As the current aim of my tutorial is to import the data from PC to R, please don’t do this now. From the above screenshot you could see the global environment window, your imported datasets will  be shown here

image.png

Recently R has introduced specific options for importing spreadsheets of common formats. You could easily scroll the dropdown menu of button in title bar “import dataset”. By clicking on this you can get the options on extra plugins to download. I am going to tell a different method, ie,by using code. Most R users like this, because of its convenience in automations. In this one is an inbuilt function and another one  you have to download. Both are essential for exploring the R world .

read.csv

The format .csv is the simple and default means of data in R. read.csv  is the command  which i commonly use because of its faster processing time.The only demerit that I found is that you could only manage one sheet of data. Let’s have a look on how it works.

Before you do, you have to set a working directory, in which you could define a folder where you could access your datasets.

getwd()# check your current directory 
[1] "/home/ecosmith/Dropbox/R_DAT"
setwd("~/Dropbox/R_DAT/lessons/")# set your working dir. 
where your data-sets are kept.

image.pngYou can do the same things by using the UI on the right bottom of R studio. Go to filebrowser>click more> set your current directory as working directory. You can view the files and your datasets. The code is given below

list.files()# get your 
file list from working directory

Let’s start the importing of .csv data. Here I want to import the data file “arrestUSA.csv”.

read.csv("arrestUSA.csv",header = TRUE)# simple method

Give a name for save and displaying  it on global environment

arrest<- read.csv("arrestUSA.csv",header = TRUE)# simple method

You could see that this data-sets are about arrests for four different crimes. You could scroll more options on read.csv by putting a coma, keep the cursor on right side of comma and click the tab button.

image.png

Now you could see that the imported data set is shown as the name “arrest” on global environment.Get the preview by simply clicking on the data or write

View(arrest)# View on the preview window

read_excel()

This is the another one, in which you could import xl spreadsheets, including sheets you defined. I found that sometimes this function runs too slow. But this one is my favorite, usually deals with MS office excel files.   Before going to use this, you need to install read_xl libraries. For this take the cursor to right bottom of rstudio window.

Go to packages>click install> then write readxl  and click again install button.

then mount the library

library(readxl)

read_execl have almost same syntax as read.csv , but differ in a couple of features like no. of sheets etc. Now we are going to start with the data car.xlsx.

car_data<- read_xlsx("car.xlsx",sheet = 1)

play on the options by clicking the tab after cursor will help you to do more features.

image.png

I hope this would be an easy tutorial to start with. Not only the spreadsheets,  you could import any kind of data-sets, including multimedia. More advanced tutorials will come soon. If you are beginner please follow the R tutorials(R spells) in the order. All kinds of comments are well appreciated.

 

 

2 thoughts on “Import spreadsheets datasets to R

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s