My Coding > Software > R > R data.frame

R data.frame

Data.frame in R is one of the data types in R. This is most common data type in R and it is very important to know how to use it. From one hand it is matrix of different type of objects, but from other side is a structure with rows and columns.

Data.frame creation in R

The simplest way to create data.frame is to join few vectors together


> coffe <- data.frame(
+ country = c("aaaa", "bbbb", "cccc", "dddd"),
+ product = c(33.3, 22.2, 44.4, 11.1),
+ percent = c(30, 20, 40, 10),
+ increas = c(F, F, T, F))
> coffe
  country product percent increas
1    aaaa    33.3      30   FALSE
2    bbbb    22.2      20   FALSE
3    cccc    44.4      40    TRUE
4    dddd    11.1      10   FALSE

Extracting data from the data.frame

It is possible to extract rows and columns for data.frame

Extracting columns from the data.frame

Slice will extract column as a vector. It is possible to call it by using it's name with $ sign


> coffe[,2]
[1] 33.3 22.2 44.4 11.1
> typeof(coffe[,2])
[1] "double"
> coffe$product
[1] 33.3 22.2 44.4 11.1
> typeof(coffe$product)
[1] "double"

Extracting column as a list

Using name of the column will produce list with it’s content


> coffe["product"]
  product
1    33.3
2    22.2
3    44.4
4    11.1
> typeof(coffe[3,])
[1] "list"

Extracting rows from the data.frame

By calling slice from the data.frame it is possible to have named list


> coffe[3,]
  country product percent increas
3    cccc    44.4      40    TRUE
> typeof(coffe[3,])
[1] "list"

To extract an individual value you can use standard list use for the row. Also you can specify the exact number of column


> coffe[3,]["product"]
  product
3    44.4
> coffe[3,]$product
[1] 44.4
> coffe[3,2]
[1] 44.4

By slicing, it is possible to have a subset from the data.frame


> coffe[1:2,1:3]
  country product percent
1    aaaa    33.3      30
2    bbbb    22.2      20

Structure of data.frame

To investigate the structure of the data.frame it is possible to use two standard functions:

  • str() - To extract the structure of our data.frame
  • names() - to extract names from our data.frame


> str(coffe)
'data.frame':	4 obs. of  4 variables:
 $ country: Factor w/ 4 levels "aaaa","bbbb",..: 1 2 3 4
 $ product: num  33.3 22.2 44.4 11.1
 $ percent: num  30 20 40 10
 $ increas: logi  FALSE FALSE TRUE FALSE
> names(coffe)
[1] "country" "product" "percent" "increas"


Published: 2021-11-10 22:53:58
Updated: 2021-11-10 23:04:58

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.