When doing nested maps in R, I often need to make a small change to an input before it goes into the next map. In that case, I don't want to write a function for it, because it's a very small change, but I can't fit it into one pipe call. Is there any way to do an assignment within an anonymous function, then feed that into the next map?
# Reprex
data = list(
`3` =list(
'1' <- c(1, 2),
'2' <- c(3,4)
),
`4` = list(
'3' <- c(5,6),
'4' <- c(7,8)
)
)
# Sum up the element of each list, and then add the name of the list to the sum.
imap(data, function(stuff, name) map(stuff, function(x) sum(x) + as.numeric(name)))
# But what if I want to change the value of the name before feeding into next map?
imap(data, function(stuff, name) name = 2 * as.numeric(name)
map(stuff, function(x) sum(x) + as.numeric(name)))
This gives the error:
Error: unexpected symbol in "imap(data, function(stuff, name) name = 2 * as.numeric(name) map"
I've also tried using brackets in different places, and continue to get the same error.