0

I am trying to figure out how to get a single data frame (or a tibble) with distinct column names from this function. This code prints three separate chunks with the same Date and same column names. Column values are however different.

hello <- function(x){
  fx <- Quandl(x)

  fx <- fx %>%
    select(Date, Open, Close) %>%
    mutate(new_col = `Open` - `Close`) %>%
    select(Date, new_col)
  print(kable(head(fx), format = "rst"))
}

lapply(c(EUR, USD, RUB), fx)

I am doing it the long way via replicating the code for every input (EUR, USD, RUB). Then merging them with the function below, then converting to a tibble to get rid of the observation numbers on the left before printing via kable.

Reduce(function(x, y) merge(x, y, all = TRUE), list(EUR, USD, RUB))

Just want to see how it is possible to do easier with one function.

Thank you!

EDIT 1:

Thank you for the answers and suggestions on improving the question!

So the reproducible code looks like this:

Wheat<-"CFTC/001602_FO_ALL"
Corn<-"CFTC/002602_F_ALL"
Beans<-"CFTC/005602_F_ALL"

funds<-function(x){

  cftc<-Quandl(x)

cftc<-cftc%>%
  select(Date,`Money Manager Longs`,`Money Manager Shorts`)%>%
  mutate(Funds_Net = `Money Manager Longs`-`Money Manager Shorts`)%>%
  select(Date,Funds_Net)
print(kable(head(cftc),format = "rst"))
}

lapply(c(Wheat,Corn,Beans),funds)

The output I have is this: enter image description here

What I want as output is this: enter image description here

Thank you!

Alex1991
  • 3
  • 1
  • 2
  • 4
    Please provide a [reproducible minimal example](https://stackoverflow.com/q/5963269/8107362). Especially, provide some sample data, e.g. with `dput()`. – mnist Nov 17 '19 at 23:01
  • 2
    It's very hard to understand what you are asking without knowing what the data look like or what the Quandl() function does. – Elin Nov 18 '19 at 00:44

0 Answers0