1

I have a data frame df with one date column and 50 other columns with individual stock prices that are named after the respective stock. The first couple of columns of the data frame basically look like this:

df
Date        Adidas  Allianz  Shell  IBM  ...
2015-12-01  130     45       200    39
2015-12-02  131     46       199    40
...

I want to perform several actions (like calculating the daily return etc.) for which I always want a new column for the output.

As I want to retain an overview, my idea was to extract each stock column together with the Date column and put them in a new data frame to basically create 50 new data frames, one for each stock. Then, I would create a list of these 50 data frames to then perform all following actions with lapply-functions on the list. I do not want to type each column name into R individually, so is there any function with which I can extract one column after the other and put them into a new data frame always together with the Date column?

My new data should look kind of like this:

list

dfAdidas
Date        Adidas
2015-12-01  130
2015-12-02  131
...

dfAllianz
Date        Allianz
2015-12-01  45
2015-12-02  46
...

and so on.

Thankful for any tips or recommendations how to do this easier!

Soph2010
  • 563
  • 3
  • 13

2 Answers2

3

Use lapply and cbind every column with the first column. It will return you a list of dataframes.

lapply(df[-1], function(x) cbind(df[1], x))

#$Adidas
#        Date   x
#1 2015-12-01 130
#2 2015-12-02 131

#$Allianz
#        Date  x
#1 2015-12-01 45
#2 2015-12-02 46

#$Shell
#        Date   x
#1 2015-12-01 200
#2 2015-12-02 199

#$IBM
#        Date  x
#1 2015-12-01 39
#2 2015-12-02 40

Or using Map

Map(cbind.data.frame, x = df[1], y = df[-1])

The alternative purrr versions using map and map2 would be

purrr::map(df[-1], ~cbind(df[1], y = .))
purrr::map2(df[-1], df[1], cbind.data.frame)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

An option with base R is to loop over the index and use that index for subsetting columns

lapply(2:ncol(df), function(i) df[c(1, i)])
#[[1]]
#        Date Adidas
#1 2015-12-01    130
#2 2015-12-02    131

#[[2]]
#        Date Allianz
#1 2015-12-01      45
#2 2015-12-02      46

#[[3]]
#        Date Shell
#1 2015-12-01   200
#2 2015-12-02   199

#[[4]]
#        Date IBM
#1 2015-12-01  39
#2 2015-12-02  40

data

df <- structure(list(Date = c("2015-12-01", "2015-12-02"), Adidas = 130:131, 
Allianz = 45:46, Shell = 200:199, IBM = 39:40),
 class = "data.frame", row.names = c(NA, -2L))
akrun
  • 874,273
  • 37
  • 540
  • 662