-
Book Overview & Buying
-
Table Of Contents
R for Data Science Cookbook
By :
In addition to generating random samples with the sample function or from any underlying probability distribution, one can sample the subset of a given dataset. In this recipe, we will introduce how to sample a subset from a financial dataset.
In this recipe, you need to prepare your environment with R installed and a computer that can access the Internet.
Please perform the following steps to sample a subset from a given financial dataset:
First, install and load the quantmod package:
> install.packages("quantmod") > library(quantmod)
Next, we can use getSymbols to obtain the Dow Jones Industrial Average data, and then subset the 2014 data from the population:
> getSymbols('^DJI') > stock = DJI['2014']
You can then randomly choose 100 samples from the dataset:
> set.seed(5) > sub <- stock[sample(nrow(stock), 100), ]
Furthermore, you can calculate the daily change from the generated subset:
> sub$change = (sub$DJI.Close...
Change the font size
Change margin width
Change background colour