Search code examples
rdataframequantmod

Adding Function Calculated Column to List of Data Frames


I have started my R learning by using a loop with the quantmod package. Essentially, I'm looping through a vector of stock symbols to download a csv of data from Yahoo, then applying the SMA (sma10 <- SMA(data[c('Adj.Close')],n=10) function to add a column to the data, before finally appending the data to the previously run stocks, creating a very large data frame.

I believe this is extremely inefficient, and have recently discovered creating a list of dataframes, which I believe will be much more efficient. But I'm struggling to understand how to replicate the adding of a calculated SMA column to each row, of each dataframe in my list. I think it requires the use of lapply, or one of the similar apply functions but I just can't figure it out.


Solution

  • I couldn't properly understand your problem. If you want to add a column to all data frames in a list, you can do like this

    List <- lapply(List,function(x) x$sma <- sma10;return(x))
    

    A reproducible example will help