hadley / adv-r

Advanced R: a book

Home Page:http://adv-r.hadley.nz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Synching exercises with AdvRS: Undefined name in Exercise 3 (section 9.4.6)

Tazinho opened this issue · comments

Exercise (9.4.6 -> 3) asks to compare two approaches where the 2nd one ins't self-contained (vars is not defined).
The exercise reads:

Explain how the following code transforms a data frame using functions stored in a list.

trans <- list(
  disp = function(x) x * 0.0163871,
  am = function(x) factor(x, labels = c("auto", "manual"))
)

nm <- names(trans)
mtcars[nm] <- map2(trans, mtcars[nm], function(f, var) f(var))

Compare and contrast the map2() approach to this map() approach:

mtcars[vars] <- map(vars, ~ trans[[.x]](mtcars[[.x]]))

Could you use nm also in the second approach or just define vars there?

Yeah, just use nm, I think.