Data Frame

By Salerno | December 18, 2019

Data Frame

This format is usually used when the information is not contained in just one dimension (vector)

Example

product <- c("Product A", "Product B", "Product C", "Product D", "Product E")

price <- c(5, 15, 4, 6, 8)

table_price_product <- data.frame(product, price)

table_price_product
##     product price
## 1 Product A     5
## 2 Product B    15
## 3 Product C     4
## 4 Product D     6
## 5 Product E     8

Indexing

Access the D Product in the Products Table:

table_price_product[4,2]
## [1] 6

Acces the Products D and E:

table_price_product[4:5,2]
## [1] 6 8

Acces a specific column in a data frame:

table_price_product$product
## [1] Product A Product B Product C Product D Product E
## Levels: Product A Product B Product C Product D Product E
table_price_product[,"product"]
## [1] Product A Product B Product C Product D Product E
## Levels: Product A Product B Product C Product D Product E

Creating a new column:

table_price_product$quantity <- c(50, 100, 120, 150, 200)
table_price_product
##     product price quantity
## 1 Product A     5       50
## 2 Product B    15      100
## 3 Product C     4      120
## 4 Product D     6      150
## 5 Product E     8      200

Or as weel you can use this format:

table_price_product[, "costs"] <- c(2, 12, 3, 5, 6)

Knowing the column’s names:

names(table_price_product)
## [1] "product"  "price"    "quantity" "costs"

Data Frame summary:

summary(table_price_product)
##       product      price         quantity       costs     
##  Product A:1   Min.   : 4.0   Min.   : 50   Min.   : 2.0  
##  Product B:1   1st Qu.: 5.0   1st Qu.:100   1st Qu.: 3.0  
##  Product C:1   Median : 6.0   Median :120   Median : 5.0  
##  Product D:1   Mean   : 7.6   Mean   :124   Mean   : 5.6  
##  Product E:1   3rd Qu.: 8.0   3rd Qu.:150   3rd Qu.: 6.0  
##                Max.   :15.0   Max.   :200   Max.   :12.0

Number os rows:

nrow(table_price_product)
## [1] 5

Number os columns:

ncol(table_price_product)
## [1] 4

Dimension’s table:

dim(table_price_product)
## [1] 5 4
comments powered by Disqus