Search code examples
rfor-loopmap-function

R calculate a new column for multiple dataframes with a map-function or a loop


I have a problem with creating a new column for multiple dataframes with a map function or a for-loop. I have 25 dataframes with cryptocurrency time series data:

ls(pattern="USD")
[1] "ADA.USD"   "BCH.USD"   "BNB.USD"   "BTC.USD"   "BTG.USD"   "DASH.USD"      "DOGE.USD"  "EOS.USD"   "ETC.USD"   "ETH.USD"   "IOT.USD"
[12] "LINK.USD"  "LTC.USD"   "NEO.USD"   "OMG.USD"   "QTUM.USD"  "TRX.USD"   "USDT.USD"  "WAVES.USD" "XEM.USD"   "XLM.USD"   "XMR.USD"
[23] "XRP.USD"   "ZEC.USD"   "ZRX.USD" 

Every object is a dataframe which stands for a cryptocurrency expressed in USD. And every dataframe has 2 columns: Date and Close (Closing price). For example: the dataframe "BTC.USD" stands for Bitcoin in USD:

head(BTC.USD)
# A tibble: 6 x 2
Date       Close
1 2015-12-31  430.
2 2016-01-01  434.
3 2016-01-02  434.
4 2016-01-03  431.
5 2016-01-04  433.

Now I want to add a third column, which represents the daily return.

require(quantmod)
BTC.USD <- BTC.USD%>%mutate(Return= Delt(Close)*100)

For a single object (in this case Bitcoin [BTC.USD]) this code works as imagined:

> head(BTC.USD)
# A tibble: 6 x 3
  Date       Close Return[,"Delt.1.arithmetic"]
  <date>     <dbl>                        <dbl>
1 2015-12-31  430.                      NA     
2 2016-01-01  434.                       0.940 
3 2016-01-02  434.                      -0.0622
4 2016-01-03  431.                      -0.696 
5 2016-01-04  433.                       0.608 
6 2016-01-05  431.                      -0.489 

Now I want to calculate the return for all 25 dataframes (or cryptocurrencies) with a map-function or a for-loop, but my code doesn't work:

temp = ls(pattern=".USD")
map(.x= temp,.f = mutate(Return= Delt(Close)*100))

Error in is.data.frame(.data) || is.list(.data) || is.environment(.data) : argument ".data" is missing, with no default

for (i in seq_along(temp)) {mutate(Return= Delt(Close)*100)}

Error in is.data.frame(.data) || is.list(.data) || is.environment(.data) : argument ".data" is missing, with no default

Can someone help me?


Solution

  • First, we need to actually get the data as a list (each data.frame will get its own entry in the list). Then, we can use any of our favorite list iterating functions to get the desired result.

    temp_data <- lapply(ls(pattern = "USD"), get) # get data into a list
    temp_data2 <- lapply(temp_data, function(x) mutate(x, Return = Delt(Close)*100))
    

    As @akrun noted, there is a more compact way to do this:

    lapply(mget(ls(pattern = "USD")), transform, Return = Delt(Close) * 100)
    

    If you want to stick with tidyverse verbs, that would be:

    lapply(mget(ls(pattern = "USD")), function(x) x %>% mutate(Return = Delt(Close) * 100))
    

    I could get the code to work using your sample data:

    sdat1 <-structure(list(
          Date = c("2015-12-31","2016-01-01",
                   "2016-01-02","2016-01-03","2016-01-04"),
          Close = c(430, 434, 434, 431, 433)),
        class = "data.frame",
        row.names = c("1", "2", "3", "4", "5"))
    
    sdat4 <- sdat3 <- sdat2 <- sdat1
    
    lapply(mget(ls(pattern = 'sdat')),
           FUN = function(x) x %>% mutate(Return = Delt(Close)))