如何在R中在嵌套函数中使用字符串和字符串列表。

huangapple go评论59阅读模式
英文:

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&#39;ve read the below and tried new things but still couldn&#39;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 &lt;- function(data, from_col, delta_col, sort_col, arg_col) {
  data %&gt;%
    mutate(across(
      all_of(unlist(delta_col)),
      ~ !!ensym(from_col) - .x
    )) %&gt;%
    arrange(desc(.data[[sort_col]])) %&gt;%
    mutate(test_col = if_else(price &gt; arg_col, 1, 0))
}

nested_df %&gt;%
  mutate(model = pmap(
    list(
      data,
      from_col,
      delta_col,
      sort_col,
      arg_col
    ),
    fun
  ))
#&gt; # A tibble: 5 &#215; 7
#&gt; # Groups:   cut [5]
#&gt;   cut       data                  sort_col from_col  delta_col  arg_col model   
#&gt;   &lt;ord&gt;     &lt;list&gt;                &lt;chr&gt;    &lt;list&gt;    &lt;list&gt;       &lt;dbl&gt; &lt;list&gt;  
#&gt; 1 Ideal     &lt;tibble [21,551 &#215; 9]&gt; table    &lt;chr [1]&gt; &lt;list [2]&gt;     500 &lt;tibble&gt;
#&gt; 2 Premium   &lt;tibble [13,791 &#215; 9]&gt; table    &lt;chr [1]&gt; &lt;list [2]&gt;     500 &lt;tibble&gt;
#&gt; 3 Good      &lt;tibble [4,906 &#215; 9]&gt;  table    &lt;chr [1]&gt; &lt;list [2]&gt;     500 &lt;tibble&gt;
#&gt; 4 Very Good &lt;tibble [12,082 &#215; 9]&gt; table    &lt;chr [1]&gt; &lt;list [2]&gt;     500 &lt;tibble&gt;
#&gt; 5 Fair      &lt;tibble [1,610 &#215; 9]&gt;  table    &lt;chr [1]&gt; &lt;list [2]&gt;     500 &lt;tibble&gt;

huangapple
  • 本文由 发表于 2023年2月24日 05:15:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定