sa90210
sa90210

Reputation: 585

How to change the value of an element within a list using purrr (tidyverse)

If I have the following list

list1 <- list(list(a = 2, b = 3), list(c = 4, d = 5))
list2 <- list(e = "a", f = "b")
mylist <- list(list1, list2)

What is the easiest way to change the value of a within mylist to a different value (preferably in purrr)?

Upvotes: 0

Views: 444

Answers (3)

James N
James N

Reputation: 325

Or try purrr::modify_in. This will return the mylist object with the updated a value.

purrr::modify_in(mylist, c(1, 1, "a"), "new_value")

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388862

Maybe you can use pluck :

purrr::pluck(mylist, 1, 1, 'a') <- 'new_value'

Upvotes: 1

FrsLry
FrsLry

Reputation: 417

Something like:

list1[[1]][[1]] <- newvalue

Upvotes: 1

Related Questions