简体   繁体   中英

R - Specify column name in function model using character strings

I am trying to perform an STL decomposition over multiple columns in a dataframe using the feasts package by Rob Hyndman (Forecasting Principles and Practice edition 3 author).

The code below works

dcmp1 <- a_tibble %>%
  model(stl = STL(Cost))
components(dcmp1)

But now I want to generate multiple decompositions for different variables within a_tibble using a for loop. The problem I face is that I iterate over the names of the columns as if they were strings, and that yields an error

Specfically, I am running this code:

for (col_name in c("Cost", "sqrt_cost")){
  
  dcmp <- model(a_tibble, stl = STL(.data[[col_name]]))

}

This yields the following warning everytime the statement within the for loop is run:

Warning: 1 error encountered for stl
[1] values must be length 1,
 but FUN(X[[1]]) result is length 0

I have tried multiple approaches to be able make decompositions for multiple variables using iteration but to no avail.

Couls someone please let me know how I should iterate to avoid this problem?

Thanks

Ok, so I resorted to this post here:

dplyr - using column names as function arguments

It solved the issue. And it made me feel like I would need to study quite in depth this "quosure" thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM