I'm trying to randomly select 2 stocks out of the Swiss Market Index, which contains of 30 stocks.

Until now I solved the random pick of the 2 stocks with the following code:

```SMI_components <- cbind("ABB (ABBN.VX)", "ADECCO (ADEN.VX)", "ACTELION (ATLN.VX)", "JULIUS BAER GRP (BAER.VX)", "RICHEMONT (CFR.VX)", "CREDIT SUISSE (CSGN.VX)", "GEBERIT (GEBN.VX)", "GIVAUDAN (GIVN.VX)", "HOLCIM (HOLN.VX)", "NESTLE (NESN.VX)", "NOVARTIS (NOVN.VX)", "TRANSOCEAN (RIGN.VX)", "ROCHE HOLDING (ROG.VX)", "SWISSCOM (SCMN.VX)", "SGS (SGSN.VX)", "SWISS RE (SREN.VX)", "SYNGENTA (SYNN.VX)", "UBS (UBSG.VX)", "SWATCH GROUP (UHR.VX)", "ZURICH INSURANCE GROUP (ZURN.VX)")

for(i in 1:1){
print(sample(SMI_components, 2))
}```

How do I continue my code, if I want to download the historical data from these two random picked stocks?

Nov 5, 2018 583 views

## 1 answer to this question.

assign the output of sample into a variable.

`my_picks <- sample(SMI_components, 2)`

Extract ticker symbol between parens (courtesy the comment below):

`my_picks <- sub(".*\\((.*)\\).*", "\\1", my_picks)`

Then you can use lapply, to call a function (yahooSeries) for each value in my_picks.

`series_list <- lapply(my_picks, yahooSeries, from = "2005-01-01", to = "2015-07-30", frequency = "daily")`

Then you'll get the output in a list. series_list[[1]] will have the output of yahooSeries for the first value of my_picks, and series_list[[2]] for the second

• 52,360 points

## How to edit the data source from R?

Use edit function like below and pass ...READ MORE

## How to Identify comman pattern of the Products that sells more from a sales data base.

Hi, If you share a real database, then ...READ MORE

## How to import data from a Json file into R?

Easy and simple: library("rjson") json_file <- "file_name" json_data <- fromJSON(file=json_file)) or ...READ MORE

## By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

## How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

## In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

## How to create a list of Data frames?

Basically all we have to do is ...READ MORE