A few list creating functions

Introduction to R for Finance

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

split() it up

debt
  name payment
1  Dan     100
2  Dan     200
3  Dan     150
4  Rob      50
5  Rob      75
6  Rob     100
Introduction to R for Finance

split() it up

grouping <- debt$name

split_debt <- split(debt, grouping)

split_debt
$Dan
  name payment
1  Dan     100
2  Dan     200
3  Dan     150

$Rob
  name payment
4  Rob      50
5  Rob      75
6  Rob     100
Introduction to R for Finance

split() it up

split_debt$Dan
  name payment
1  Dan     100
2  Dan     200
3  Dan     150
split_debt$Dan$payment
100 200 150
unsplit(split_debt, grouping)
  name payment
1  Dan     100
2  Dan     200
3  Dan     150
4  Rob      50
5  Rob      75
6  Rob     100
Introduction to R for Finance

split() example

  • Unique calculation for Dan versus Rob
  • Dan gets a 20% discount, Rob a 10% discount
    • split data frame by name
    • apply discounts
    • combine data frames back
  • "split-apply-combine"
Introduction to R for Finance

split-apply-combine

split_debt <- split(debt, grouping)
grouping <- debt$name

split_debt$Dan$new_payment <- split_debt$Dan$payment * .8 split_debt$Rob$new_payment <- split_debt$Rob$payment * .9
split_debt
$Dan
  name payment new_payment
1  Dan     100          80
2  Dan     200         160
3  Dan     150         120

$Rob
  name payment new_payment
4  Rob      50        45.0
5  Rob      75        67.5
6  Rob     100        90.0
Introduction to R for Finance

split-apply-combine

unsplit(split_debt, grouping)
  name payment new_payment
1  Dan     100        80.0
2  Dan     200       160.0
3  Dan     150       120.0
4  Rob      50        45.0
5  Rob      75        67.5
6  Rob     100        90.0
Introduction to R for Finance

Attributes

my_matrix <- matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3)

attributes(my_matrix)
$dim
2 3
attributes(debt)
$names
"name"    "payment"

$row.names
1 2 3 4 5 6

$class
"data.frame"
Introduction to R for Finance

Let's practice!

Introduction to R for Finance

Preparing Video For Download...