I need to convert an Excel column into a list of vectors in R. The column only has stocks symbols names like APPL, GOOG, MSFT, etc. But many of them.

I accomplished to create a dataframe with the names of the stock but no success in creating a list the way I need.

That's what I got:

 list_of_stocks <- read.table(file = "clipboard")
 head(list_of_stocks)
      V1
  1 APPL
  2 GOOG
  3 MSFT
  4 TTNP
  5 AVCO
  6 CCCL

As you can see it is structured as a data.frame.

str(list_of_stocks)
'data.frame':   123 obs. of  1 variable:
$ V1: Factor w/ 74 levels "APPL","ACHV",..: 32 57 2 68 10 18 71 8 71 69 ...

That's what I need:

list_of_stocks 
    [1] "APPL" "GOOG" "MSFT" "TTNP" "AVCO" "CCCL"

I will be using this in the getSymbols function, so I can download multiple stocks in one time in a enviroment. I want to be able to use it like this:

getSymbols(list_of_stocks, env = stocks_research, auto.assign = TRUE)

Any help?

1 Answers

0
akrun On Best Solutions

What we find is the default behavior of read.table/read.csv or data.frame where the column types get automatically converted to factor if there is any string element as stringsAsFactors = TRUE. Inorder to reset it, use stringsAsFactors = FALSE and then extract the column

list_of_stocks <- read.table(file = "clipboard", stringsAsFactors = FALSE)
list_of_stocks$V1