英文:
how to use strings and listed strings in nested functions in R
问题
以下是您要翻译的内容:
"I am still trying to improve my understanding how to create a grid table and reference columns either in the grid table or columns in a nested dataset but running into issues with NSE.
I've read the below and tried new things but still couldn't get it to work.
https://dplyr.tidyverse.org/articles/programming.html
Ultimately, I am trying to figure how to use columns from a grid table to identify columns that are nested in a table.
library(tidyverse)
#create tables with columns
sort_tbl <- tibble(sort_col="table")
from_col <- list("x")
delta_col <- list("y","z")
#add columns including arguments column and nest dataset
nested_df <- diamonds %>%
group_by(cut) %>%
nest() %>%
mutate(sort_col="table",
from_col=list("x"),
delta_col=list(delta_col),
arg_col=500
)
#create function that takes input columns from grid table and try to add to dataset
fun <- function(data,from_col,delta_col,sort_col,arg_col) {
data %>%
mutate(across(!!ensym(delta_col),
~!!ensym(from_col)-.x)) %>%
arrange(desc({sort_col})) %>%
mutate(test_col=if_else(price>{arg_col},1,0))
}
#try to add it and but this doesn't work
nested_df %>%
mutate(model=pmap(list(data,
from_col,
delta_col),
~fun(data=data,
from_col=from_col,
delta_col=delta_col,
arg_col=arg_col)
)
)"
请注意,代码部分没有进行翻译。
<details>
<summary>英文:</summary>
I am still trying to improve my understanding how to create a grid table and reference columns either in the grid table or columns in a nested dataset but running into issues with NSE.
I've read the below and tried new things but still couldn't get it to work.
https://dplyr.tidyverse.org/articles/programming.html
Ultimately, I am trying to figure how to use columns from a grid table to identify columns that are nested in a table.
library(tidyverse)
#create tables with columns
sort_tbl <- tibble(sort_col="table")
from_col <- list("x")
delta_col <- list("y","z")
#add columns including arguments column and nest dataset
nested_df <- diamonds %>%
group_by(cut) %>%
nest() %>%
mutate(sort_col="table",
from_col=list("x"),
delta_col=list(delta_col),
arg_col=500
)
#create function that takes input columns from grid table and try to add to dataset
fun <- function(data,from_col,delta_col,sort_col,arg_col) {
data %>%
mutate(across(!!ensym(delta_col),
~!!ensym(from_col)-.x)) %>%
arrange(desc({sort_col})) %>%
mutate(test_col=if_else(price>{arg_col},1,0))
}
#try to add it and but this doesn't work
nested_df %>%
mutate(model=pmap(list(data,
from_col,
delta_col),
~fun(data=data,
from_col=from_col,
delta_col=delta_col,
arg_col=arg_col)
)
)
</details>
# 答案1
**得分**: 2
以下是代码部分的翻译:
``` r
library(tidyverse)
fun <- function(data, from_col, delta_col, sort_col, arg_col) {
data %>%
mutate(across(
all_of(unlist(delta_col)),
~ !!ensym(from_col) - .x
)) %>%
arrange(desc(.data[[sort_col]])) %>%
mutate(test_col = if_else(price > arg_col, 1, 0))
}
nested_df %>%
mutate(model = pmap(
list(
data,
from_col,
delta_col,
sort_col,
arg_col
),
fun
))
#> # A tibble: 5 × 7
#> # Groups: cut [5]
#> cut data sort_col from_col delta_col arg_col model
#> <ord> <list> <chr> <list> <list> <dbl> <list>
#> 1 Ideal <tibble [21,551 × 9]> table <chr [1]> <list [2]> 500 <tibble>
#> 2 Premium <tibble [13,791 × 9]> table <chr [1]> <list [2]> 500 <tibble>
#> 3 Good <tibble [4,906 × 9]> table <chr [1]> <list [2]> 500 <tibble>
#> 4 Very Good <tibble [12,082 × 9]> table <chr [1]> <list [2]> 500 <tibble>
#> 5 Fair <tibble [1,610 × 9]> table <chr [1]> <list [2]> 500 <tibble>
请注意,这是代码部分的翻译,我已经去除了不需要翻译的部分。如果您有任何其他问题或需要进一步的帮助,请随时提出。
英文:
There are four issues with your code. First, in pmap
you have to loop over all five columns used as arguments of your function. Second, doing fun(data=data, ...
) your are passing the whole column to your function instead of just one element. Third, delta_col
is a list
so you have to unlist()
it in across
. Finally, as sort_col
is a a character use the .data
pronoun in arrange
:
library(tidyverse)
fun <- function(data, from_col, delta_col, sort_col, arg_col) {
data %>%
mutate(across(
all_of(unlist(delta_col)),
~ !!ensym(from_col) - .x
)) %>%
arrange(desc(.data[[sort_col]])) %>%
mutate(test_col = if_else(price > arg_col, 1, 0))
}
nested_df %>%
mutate(model = pmap(
list(
data,
from_col,
delta_col,
sort_col,
arg_col
),
fun
))
#> # A tibble: 5 × 7
#> # Groups: cut [5]
#> cut data sort_col from_col delta_col arg_col model
#> <ord> <list> <chr> <list> <list> <dbl> <list>
#> 1 Ideal <tibble [21,551 × 9]> table <chr [1]> <list [2]> 500 <tibble>
#> 2 Premium <tibble [13,791 × 9]> table <chr [1]> <list [2]> 500 <tibble>
#> 3 Good <tibble [4,906 × 9]> table <chr [1]> <list [2]> 500 <tibble>
#> 4 Very Good <tibble [12,082 × 9]> table <chr [1]> <list [2]> 500 <tibble>
#> 5 Fair <tibble [1,610 × 9]> table <chr [1]> <list [2]> 500 <tibble>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论