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)
print(df)

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:
print(df[1:2,])
# accessing first & second column:
print(df[,1:2])
# accessing 1st & 2nd column -
# alternate:
print(df[1:2])

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.




0 Comments:

Post a Comment

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (893) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses