Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question / New Feature Request: Looking for a way to update a subset of list elements based on a condition #135

Open
wdm0336 opened this issue Apr 7, 2023 · 0 comments

Comments

@wdm0336
Copy link

wdm0336 commented Apr 7, 2023

Love the package but I keep running into the need to change a field in only a subset of the elements in the list. Is there a way to do this today?

For example, something like:

foo <- list(
    list(a = 1, b = 10),
    list(a = 2, b = 20),
    list(a = 3, b = 30)
)

# change field 'b' to 300 in each list element where field 'a' is a 3
foo <- foo |>
    rlist::list.update_with_condition(a == 3, b = 300)

giving:

list(
    list(a = 1, b = 10),
    list(a = 2, b = 20),
    list(a = 3, b = 300)
)

Today, I end up doing it with lapply:

# change field 'b' to 300 in each list element where field 'a' is a 3
foo <- lapply(foo, function(x) {
    if (x$a == 3) {
        x$b <- 300
    }
    x
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant