See below
for(stockName in c("AAPLstock", "FBstock", "GOOGstock", "HPQstock", "MSFTstock", "YHOOstock")){
assign(paste(stockName), read.table(paste(stockName,'.txt', sep = ""), quote="\"", comment.char="")) }
This creates these variables. Which is all cool
ls(pattern = "stock")
[1] "AAPLstock" "FBstock" "GOOGstock" "HPQstock" "MSFTstock"
[6] "stockName" "YHOOstock"
When I try to assign all of them to a data frame like below
df<-list(ls(pattern = "stock"))
> df
[[1]]
[1] "AAPLstock" "FBstock" "GOOGstock" "HPQstock" "MSFTstock"
[6] "stockName" "YHOOstock"
What I want is for df to contain these stocks as variables, not as strings. So the function below gives me the desired result
df <- list(AAPLstock, FBstock, GOOGstock, HPQstock, MSFTstock, YHOOstock)
But that requires me to type out all the variables. The thing is, I am going to be doing that but with more stocks, and I don't want to type the name of a 100 variables, and it's going to be a pain to change. So I want a way to assign strings that represent variables to a data frame using ls() or something similar.
I have tried Parse and assign, but they still assigned df strings rather than variables.
lapply(stockNameVector, function(stockName) read.table(paste(... )