Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 30

R – Data Frames

 A Data Frame is a table or a 2-D array like


structure in which each column contains
values of one variable and each row
contains one set of values from each
column.
Data Frame

 Characteristics of Data Frame


 Column names should be non-empty
 Row names should be unique
 Data stored in a data frame can be of
numeric, factor or character type.
 Each column should contain same number of
data items.
Create Data Frame
Create Data Frame
Get the structure of the
data frame

 Structure of the data frame can be seen


by using str() function.
Summary of Data in Data
Frame

 Statistical summary and nature of the


data can be obtained by applying
summary() function.
Extract data from data
frame

 Extract specific column from a data frame


using column name.
Extract data from data
frame

 Extract first three rows and all columns


Extract data from data
frame

 Extract 3rd and 4th row with 2nd and 3rd


column
Expand Data Frame

 Data frame can be expanded by adding


rows and columns.
 To add column
 Just add the column vector using new column
name.
Expand Data Frame
Expand Data Frame

 To add new rows:


 To add more rows permanently to an existing
data frame, put the new rows in the same
structure as existing data frame and use the
rbind() function.
Expand Data Frame
R- Working with CSV files

 In R, we can read data from files stored


outside the R environment.
 We can also write data into files which will
be stored and accessed by the Operating
system.
 R can read and write into various file
formats like .csv, .xls, .xml, etc
 To read data from a .csv (comma
separated value) and then write data into
a .csv file, the file should be present in
the current working directory so that R
can read it.
 getwd() – to get your current working
directory.

 setwd() – to set your new working


directory.
Input as CSV file

 CSV is a text file in which the values in the


columns are separated by a comma.
 Create the file by using windows notepad.
 Save this file as input.csv using the Save
As – All Files (*.*) option in notepad.
Reading a CSV file

 To read a .csv file available in your


current working directory use read.csv()
function.
Analyzing the .CSV file

 By default, the read.csv() function gives


the output as a data frame.
 To check whether it is stored as a data
frame

 To check the number of columns and


number of rows
Analyzing the .CSV file

 Get the maximum age:


Analyzing the .CSV file

 Get the details of the student with the maximum


age:
 We can fetch rows meeting specific filter criteria
similar to a SQL where clause.
Method - I

Method - II
Analyzing the .CSV file

 Get all the students in the HR department


Analyzing the .CSV file

 Get students in IT department whose age


is greater than 25.
Writing into a CSV file

 R can create .csv file from existing data


frame.
 The write.csv() function is used to create
the .csv file.
 This file gets created in the working
directory.
Writing into a CSV file

Here the column X comes from the data set. This can be dropped.
Writing into a CSV file
R-Excel file

 Microsoft excel is the most widely used


spreadsheet program which stores data
in .xls or .xlsx format.
 R can read directly from these files using
some excel specific packages.
 Few such packages are XLConnect, xlsx,
gdata, etc.
 We will be using xlsx package.
Install .xlsx package

install.packages(“xlsx”)
 Load the library file

 Create an Excel file with two sheets

Rno Age Name Dept Name City


1 26Aarya IT Aarya Kochi
2 25Siddharth HR Siddharth Chennai
Tarush Mumbai
3 24Tarush Finance Diva Delhi
4 24Diva IT Richa Jaipur
5 25Richa HR
Save the excel file as “input.xlsx”.
Sheet Name : City
Sheet Name : Employee
Reading the Excel file

 Use read.xlsx() function to read the excel


file.
 To read the input.xlsx file and first sheet
use the following command:

You might also like