0

So I would like to create a matrix of data in R using a function which I call setupdata. Basically getSymbols get stock data for the given symbols but from a loooong time. I wish to select the latest 60 trading days and therefore has design the following code.

getSymbols(c("GOOG","RBS.L","AAPL","FB"),src="yahoo")

setupdata<- function (x){
data2<-x[(dim(x)[1]-59):dim(x)[1],1:6]
print(data2)
}

setupdata(AAPL)

My problem is that i would like to be able to use "data2" without needing to call the function anymore after running it once. What am i missing out?

If you want to try it for yourself you might need the quantmod library

install.packages("quantmod")
library("quantmod")

1 Answer 1

2

You can simply return data2 from the function:

setupdata<- function (x){
    data2 <- x[(dim(x)[1]-59):dim(x)[1],1:6]
    return(data2)
}

data2_outside_function = setupdata(AAPL)
Sign up to request clarification or add additional context in comments.

1 Comment

The return statement isn't actually needed. You just have to assign the value of setupdata(AAPL) to some object, so you can reuse it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.