Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

How to dynamically rename columns in a function in R?

$
0
0

Here is a function I'm trying to build in R

lag_decay_gen <- function(data,col,lag_val,decay_val)
  {
  temp <- lag(col, n = lag_val*7, default =0)
  temp <- temp + shift(temp)*decay_val
  temp <- cbind(data,temp)
  }

Here data = main dataset while col = column of the dataset

Generally, the input for col would something look like this - data$'column_name'

Here inside the function I would like the temp column to be renamed dynamically to

paste(col,lag_val,decay_val, sep = "_")

where col is the name of the column but this is the value under the column that I'm passing to the function and hence an error occurs.

Moreover, how should I go about building the function lag_decay_gen if my col input has multiple columns. Any help would be much appreciated.


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>