18 Списки [EN]
18.1 What is lists in R?
Lists are the R objects which contain elements of different types like − numbers, strings, vectors and another list inside it. A list can also contain a matrix or a function as its elements. List is created using list()
function.
Before start lest see one more package for working with date lubridate
. It has a lot of functions for date parsing, manipulating and other. Check it with:
For our sample we need function ymd()
that parse charater date from format like “2012-10-25”.
You can also use ymdhms()
to parse date and time correctly.
18.2 Creating a List
Following is an example to create a list containing vectors, strings, numbers and a logical values. Our list will describe a model of banks client:
# initial values
set.seed(1) # for fixing pseudo-random
library(lubridate)
client_name <- "John Doe"
services <- c("credit", "deposite", "online-app")
is_active <- TRUE
transactions <- data.frame(contract_id = sample(10000:99999, size = 2, replace = T),# random numbers
datetime = c(ymd_hms("2021-05-25 11:05:12"),
ymd_hms("2021-05-25 11:07:14"),
ymd_hms("2021-05-25 11:08:02"),
ymd_hms("2021-05-25 11:12:45"),
ymd_hms("2021-05-25 11:47:00"),
ymd_hms("2021-05-25 11:48:08")),
oper_type = sample(0:1, size=6, replace = T), # 1 for debet, 0 for credet
amount = round(sample(1:1000, size = 6) + runif(6),2))
#change AMOUNT to minus for debet (opertype == 1
transactions$amount <- ifelse(transactions$oper_type == 1, (-1)*transactions$amount, transactions$amount)
transactions
contract_id | datetime | oper_type | amount |
---|---|---|---|
<int> | <dttm> | <int> | <dbl> |
34387 | 2021-05-25 11:05:12 | 1 | -187.72 |
69520 | 2021-05-25 11:07:14 | 0 | 307.99 |
34387 | 2021-05-25 11:08:02 | 0 | 993.38 |
69520 | 2021-05-25 11:12:45 | 0 | 597.78 |
34387 | 2021-05-25 11:47:00 | 1 | -277.93 |
69520 | 2021-05-25 11:48:08 | 1 | -874.21 |
# creating list of signle objects, vector and dataframe
list_data <- list(client_name, is_active, services, transactions)
list_data
- 'John Doe'
- TRUE
-
- 'credit'
- 'deposite'
- 'online-app'
A data.frame: 6 × 4 contract_id datetime oper_type amount <int> <dttm> <int> <dbl> 34387 2021-05-25 11:05:12 1 -187.72 69520 2021-05-25 11:07:14 0 307.99 34387 2021-05-25 11:08:02 0 993.38 69520 2021-05-25 11:12:45 0 597.78 34387 2021-05-25 11:47:00 1 -277.93 69520 2021-05-25 11:48:08 1 -874.21
18.3 Naming List Elements
Its better to name elements in list:
- $ClientName
- 'John Doe'
- $IsActive
- TRUE
- $Services
-
- 'credit'
- 'deposite'
- 'online-app'
- $Transactions
A data.frame: 6 × 4 contract_id datetime oper_type amount <int> <dttm> <int> <dbl> 34387 2021-05-25 11:05:12 1 -187.72 69520 2021-05-25 11:07:14 0 307.99 34387 2021-05-25 11:08:02 0 993.38 69520 2021-05-25 11:12:45 0 597.78 34387 2021-05-25 11:47:00 1 -277.93 69520 2021-05-25 11:48:08 1 -874.21
You can extend list “on fly” with $
:
- $ClientName
- 'John Doe'
- $IsActive
- TRUE
- $Services
-
- 'credit'
- 'deposite'
- 'online-app'
- $Transactions
A data.frame: 6 × 4 contract_id datetime oper_type amount <int> <dttm> <int> <dbl> 34387 2021-05-25 11:05:12 1 -187.72 69520 2021-05-25 11:07:14 0 307.99 34387 2021-05-25 11:08:02 0 993.38 69520 2021-05-25 11:12:45 0 597.78 34387 2021-05-25 11:47:00 1 -277.93 69520 2021-05-25 11:48:08 1 -874.21 - $ClientId
- 11125489656
18.4 Accessing List Elements
For now every element can be viewed with index in [[]]
or []
:
Access by $
also anbled:
contract_id | datetime | oper_type | amount |
---|---|---|---|
<int> | <dttm> | <int> | <dbl> |
34387 | 2021-05-25 11:05:12 | 1 | -187.72 |
69520 | 2021-05-25 11:07:14 | 0 | 307.99 |
34387 | 2021-05-25 11:08:02 | 0 | 993.38 |
69520 | 2021-05-25 11:12:45 | 0 | 597.78 |
34387 | 2021-05-25 11:47:00 | 1 | -277.93 |
69520 | 2021-05-25 11:48:08 | 1 | -874.21 |
18.5 Manipulating List Elements
Lets continue using out list_data
list.
- $ClientName
- 'John Doe'
- $IsActive
- TRUE
- $Services
-
- 'credit'
- 'deposite'
- 'online-app'
- $Transactions
A data.frame: 6 × 4 contract_id datetime oper_type amount <int> <dttm> <int> <dbl> 34387 2021-05-25 11:05:12 1 -187.72 69520 2021-05-25 11:07:14 0 307.99 34387 2021-05-25 11:08:02 0 993.38 69520 2021-05-25 11:12:45 0 597.78 34387 2021-05-25 11:47:00 1 -277.93 69520 2021-05-25 11:48:08 1 -874.21 - $ClientId
- 11125489656
We can change data with []
and access with $
symbol.
- $ClientName
- 'New Name'
- $IsActive
- TRUE
- $Services
-
- 'credit'
- 'deposite'
- 'online-app'
- $Transactions
A data.frame: 6 × 4 contract_id datetime oper_type amount <int> <dttm> <int> <dbl> 34387 2021-05-25 11:05:12 1 -187.72 69520 2021-05-25 11:07:14 0 307.99 34387 2021-05-25 11:08:02 0 993.38 69520 2021-05-25 11:12:45 0 597.78 34387 2021-05-25 11:47:00 1 -277.93 69520 2021-05-25 11:48:08 1 -874.21 - $ClientId
- 11125489656
- $ClientName
- 'John Doe'
- $IsActive
- TRUE
- $Services
-
- 'credit'
- 'deposite'
- 'online-app'
- $Transactions
A data.frame: 6 × 4 contract_id datetime oper_type amount <int> <dttm> <int> <dbl> 34387 2021-05-25 11:05:12 1 -187.72 69520 2021-05-25 11:07:14 0 307.99 34387 2021-05-25 11:08:02 0 993.38 69520 2021-05-25 11:12:45 0 597.78 34387 2021-05-25 11:47:00 1 -277.93 69520 2021-05-25 11:48:08 1 -874.21 - $ClientId
- 11125489656
Yo can merge lists with c()
function. Let’s create new list and attach it to the list_data
:
- $Name
- 'David Cameron'
- $PhoneNum
- '+9562311855'
- $ClientName
- 'John Doe'
- $IsActive
- TRUE
- $Services
-
- 'credit'
- 'deposite'
- 'online-app'
- $Transactions
A data.frame: 6 × 4 contract_id datetime oper_type amount <int> <dttm> <int> <dbl> 34387 2021-05-25 11:05:12 1 -187.72 69520 2021-05-25 11:07:14 0 307.99 34387 2021-05-25 11:08:02 0 993.38 69520 2021-05-25 11:12:45 0 597.78 34387 2021-05-25 11:47:00 1 -277.93 69520 2021-05-25 11:48:08 1 -874.21 - $ClientId
- 11125489656
- $Consultant
- $Name
- 'David Cameron'
- $PhoneNum
- '+9562311855'
With unlist()
you can convert a list to a vector.
list_demo * 5 # error, you cannot use * for list
18.6 Tasks
18.6.1 Task 1
Wrie a function that calculates sum
, average
, median
, min
, max
of taken vector. Generate sample vector of 10 elements in \([1;100]\).
Solution
18.7 References
- The Comprehensive R Archive NetworkRcran: Url: https://cran.r-project.org/
- RStudio official website. Url: https://rstudio.com/
- Anaconda official website. Url: https://www.anaconda.com/
- Introduction to R. Datacamp interactive course. Url: https://www.datacamp.com/courses/free-introduction-to-r
- Quanargo. Introduction to R. Url: https://www.quantargo.com/courses/course-r-introduction
- R Coder Project. Begin your data science career with R language! Url: https://r-coder.com/
- R Core Team (2019). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria.URL https://www.R-project.org/.
- A.B. Shipunov, EM Baldin, P.A. Volkova, VG Sufiyanov. Visual statistics. We use R! - M .: DMK Press, 2012. - 298 p .: ill.
- An Introduction to R. URL: https://cran.r-project.org/doc/manuals/r-release/R-intro.html
- R programming. https://www.datamentor.io/r-programming
- Learn R. R Functions. https://www.w3schools.com/r/r_functions.asp
- UC Business Analytics R Programming Guide. Managing Data Frames. http://uc-r.github.io/dataframes
- Learn R programming. R - Lists. https://www.tutorialspoint.com/r/r_lists.htm
- Tutorial on the R Apply Family by Carlo Fanara. https://www.datacamp.com/community/tutorials/r-tutorial-apply-family