Thursday, 9 August 2018

Dataframes in R Language

Dataframes : Create dataframe

Data frames are generic data objects of R, used to store tabular data.

Code :-

# Introduction to data frames
 vec1 = c(1,2,3)
 vec2 = c("R","Scilab","Java")
 vec3 = c("For prototyping","For prototyping","For Scaleup")
 df = data.frame(vec1,vec2,vec3)

Console Output

Create a dataframe using data from a file
  • A dataframe can also be created by reading data from a file using the following command.        
                - newDF = read.table(path="Path of the file")
  • In the path, please use '/' instead of '/' .
                - Example:  "C:/Users/hill/Documents/R/R-Workspace/"
  • A separator can also be used to distinguish between entries. Default separator is space, ' ' .
               - newDF = read.table(file="path of the file" , sep)

Accessing rows and columns
  • df[val1,val2] refers to row "val1" , column "val2" . Can be number or sting.
  • "val1" or "val2" can also be array of values like "1:2" or "c(1,3)".
  • df[val2] (no commas) - just refers to column "val2" only

Code :-

# accessing first & second row:
# accessing first & second column:
# accessing 1st & 2nd column -
# alternate:

Output :-

Subset :-

Subset( ) which extracts subset of data based on conditions.

Editing dataframes
  • A dataframe can also be edited using the edit( ) command
  • Create an instance of data frame and use edit command to open a table editor, changes can be manually made.

Adding extra rows and columns

Extra row can be added with "rbind" function and extra column with "cbind".

Deleting rows and columns

There are several ways to delete a row/column, some cases are shown below.

Manipulating rows - the factor issue
  • When character columns are created in a data .frame, they become factors
  • Factor variables are those where the character column is split into categories or factor levels.

Resolving factor issue

New entries need to be consistent with factor levels which are fixed when the dataframe is first created.


Post a Comment

Codecademy Code Foundations

Popular Posts


Android (23) AngularJS (1) Assembly Language (2) Books (10) C (75) C# (12) C++ (81) Course (1) Data Strucures (4) Downloads (1) Engineering (13) flutter (1) FPL (17) Hadoop (1) HTML&CSS (38) IS (25) Java (89) Leet Code (4) Pandas (1) PHP (20) Projects (19) Python (398) R (69) Selenium Webdriver (2) Software (14) SQL (27)