Params with vector

Hi,
i would like to create a script in R with params, can I create a params as vector?
For example:
I have this command: mutate(cluster=case_when((offert=="xxx" & prod %in% c("a","b") & year==params$year1 & month %in% c(2,3,4,5) ) ~"Cluster1")
I would like to replace c(2,3,4,5) with params$months , but if i set params with months: c(2,3,4,5) it doesn't work, cluster is NA.
How can i solve this?

Hello.
Thanks for providing code , but you could take further steps to make it more convenient for other forum users to help you.

Share some representative data that will enable your code to run and show the problematic behaviour.

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

This is an example:

Sample:

|ID|prodotto_cod|year|month|
|1|SGSTD|2022|3|
|2|SGP|2021|11|
|3|SGP|2022|2|
|4|SGE|2022|5|
|5|SGSTD|2022|5|
|6|SGE|2022|5|
|7|SGBAS|2022|3|
|8|SGPRE|2022|4|
|9|SGP|2022|3|
|10|SGBAS|2021|3|


title: "Example"
author: ""
date: "22/09/2022"
params:
year0: 2021
year1: 2022
months: c(2,3,4,5)
n_months: 4
output:
html_document: default
word_document: default


#example without params
example_without_params <- example %>% 
    mutate(cluster=case_when((prodotto_cod %in% c("SGSTD","SGBAS") & year==2022 & month %in% c(2,3,4,5)) ~"Cluster1",
                           (prodotto_cod %in% c("SGPRE") & year==2022 & month %in% c(2,3,4,5)) ~ "Cluster2"))

#what would i like do
example_with_params <- example %>% 
    mutate(cluster=case_when((prodotto_cod %in% c("SGSTD","SGBAS") & year==params$year1 & month %in% params$months) ~"Cluster1",
                           (prodotto_cod %in% c("SGPRE") & year==params$year1 & month %in% params$months) ~ "Cluster2"))
#in this case all cluster in NA, params$months doesn't work
  

Define the vector like this: !r c(2,3,4,5)

---
title: "Example"
author: ""
date: "22/09/2022"
params:
  year0: 2021
  year1: 2022
  months: !r c(2,3,4,5)
  n_months: 4
output:
  html_document: default
---
1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.