0

I have a 496 dataframes of S&P 500 stocks. each dataframe have diffrent name and column name like these

         AAL.Open  AAL.High  AAL.Low AAL.Close   AAL.Volume
09-12-13    23.85   25.44   23.45      24.6        43197268
10-12-13    24.5    25.17   24.41      24.88       18660625

         AA.Open  AA.High   AA.Low  AA.Close    AA.Volume
09-12-13    22.5    24.25   20.13     21           6639
10-12-13    21.2    21.2    20.89     21           10963

Now I want to cteare a new Dataframe of 496 columns by taking a particular column of each dataframes like this

          AAL.High    AA.High   -----   ------
09-12-13    25.44     24.25
10-12-13    25.17      21.2
Jaap
  • 81,064
  • 34
  • 182
  • 193

1 Answers1

2

We place the datasets in a list and extract the column of interest. Based on the OP's code, the column names are not exact. SO, we could either use grep

do.call(cbind, lapply(lst, function(x) x[grep("High", names(x))]))

or the column position by numeric indexing

do.call(cbind, lapply(lst, function(x) x[2]))

NOTE: Assumed that the datasets are data.frame

data

lst <- list(d1, d2)

Or with dput

lst <- list(structure(list(AAL.Open = c(23.85, 24.5), AAL.High = c(25.44, 
 25.17), AAL.Low = c(23.45, 24.41), AAL.Close = c(24.6, 24.88), 
AAL.Volume = c(43197268L, 18660625L)), .Names = c("AAL.Open", 
 "AAL.High", "AAL.Low", "AAL.Close", "AAL.Volume"), class = "data.frame", 
 row.names = c("09-12-13", 
 "10-12-13")), structure(list(AA.Open = c(22.5, 21.2), AA.High = c(24.25, 
  21.2), AA.Low = c(20.13, 20.89), AA.Close = c(21L, 21L), 
 AA.Volume = c(6639L, 
 10963L)), .Names = c("AA.Open", "AA.High", "AA.Low", "AA.Close", 
 "AA.Volume"), class = "data.frame", row.names = c("09-12-13", 
 "10-12-13")))
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Error in lapply(lst, function(x) x[grep("High", names(x))]) : object 'lst' not found – Syed Riaz Mahmood Ali Aug 29 '17 at 06:23
  • @SyedRiazMahmoodAli The `lst` is a `list` object creating by keeping all the datasets in a `list` – akrun Aug 29 '17 at 06:25
  • Thank you. Can I create a new DF in global environment. not as a value – Syed Riaz Mahmood Ali Aug 29 '17 at 06:29
  • 1
    @SyedRiazMahmoodAli To avoid creating the list manually see [this post](https://stackoverflow.com/a/25510018/680068) – zx8754 Aug 29 '17 at 06:30
  • the first two formula only giving two row and two column result like this AAL.High AA.High 09-12-13 25.44 24.25 10-12-13 25.17 21.20 – Syed Riaz Mahmood Ali Aug 29 '17 at 06:44
  • @SyedRiazMahmoodAli You only showed two dataset each with 2 rows. I am just copy/pasting your example to do this. For your original dataset, get all the dataset in list and then apply the code. Also, I am assuming that you have a `data.frame`. If it is matrix, then use `x[, 2]` – akrun Aug 29 '17 at 06:48
  • when I do this 2nd<-list('AAPL', 'ABT', 'ABBV', 'ACN',.............................) and ddf8<-do.call(cbind, lapply(2nd, function(x) x[grep("High", names(x))])) Error: unexpected symbol in "ddf8<-do.call(cbind, lapply(2nd" – Syed Riaz Mahmood Ali Aug 29 '17 at 07:19
  • @SyedRiazMahmoodAli Is that name of the object? If it is the object name then `lst <- list(AAPL, ABT, ABBV, ...)` without quotes – akrun Aug 29 '17 at 07:20
  • when I put it without quotes it shows : 3rd <-list(ABT, ABBV, ACN, ACE, ADBE, ADT, AAP, AES) Error: unexpected symbol in "3rd" – Syed Riaz Mahmood Ali Aug 29 '17 at 07:27
  • @SyedRiazMahmoodAli I think you are not giving full information. What is `ABT`, `ABBV` etc. Is it like `ABT <- data.frame(col1 = .., col2 = ...)` – akrun Aug 29 '17 at 08:00
  • ABT, ABBV, ACN ect are the 496 symbols of all s&p 500 indexed companies stock data which I have downloaded by r. Now the company symbols represents 496 Dataframes contain 5 columns each which I mentioned in the main question. I want to create a new dataframe or matrix of containing 496 coumn (2nd column) – Syed Riaz Mahmood Ali Aug 29 '17 at 08:08
  • @SyedRiazMahmoodAli Can u show how u r getting the dataset – akrun Aug 29 '17 at 08:13
  • @SyedRiazMahmoodAli If you want to download multple elements in a list, take a look at [this](https://stackoverflow.com/questions/37290727/using-lapply-to-find-individual-mean-across-multiple-dataframes-error-incorrec) and then use the code above – akrun Aug 29 '17 at 08:17
  • require(quantmod), quantmod_list <- c('AAPL', 'ABT', 'ABBV', 'ACN', 'ACE',...............) for(company in quantmod_list){ try(getSymbols(company, auto.assign = TRUE, src="google")) print(company) } – Syed Riaz Mahmood Ali Aug 29 '17 at 08:20
  • @SyedRiazMahmoodAli TRy to put it in a `list` using `lst <- lapply(symbols, function(sym) {getSymbols(sym, from=StartDate, auto.assign=FALSE)})` specify the `StartDate` if you have one. I can't execute this command as it is blocked in my work location – akrun Aug 29 '17 at 08:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153098/discussion-between-syed-riaz-mahmood-ali-and-akrun). – Syed Riaz Mahmood Ali Aug 29 '17 at 08:29
  • @SyedRiazMahmoodAli Can you check [this](https://stackoverflow.com/questions/43573018/for-loops-for-functions-containing-multiple-arguments-r/43573856) This one should work as I was able to do that at that time – akrun Aug 29 '17 at 08:36
  • 1
    @akrun It is now working. I am not sure why it was not working initially. Thank you so much for your support. – Syed Riaz Mahmood Ali Aug 29 '17 at 12:44