1
votes

Please forgive as I am new to this forum. I have a large dataframe with many columns, e.g: df(Timestamp, A, B, C, D, E, F, G). The study needs separate data.frames whose columns are extracted from main data.frame (df) e.g. df1(Timestamp, A), df2(Timestamp, B), df3(Timestamp, C) and so on. Can someone please help to create these data.frames viz. df1, df2,df3 etc. It would be great if the columns are indexed by some number instead of name as some data.frames are generated from xts objects. Sample data:

Timestamp              qs           pqs       logqs        es          p_imp          dep           r_dep
01-04-2015 09:30:00 2.389130435 0.001441341 0.001441375 2.97826087  -0.431832298    66.50621118 110181.2601
01-04-2015 10:00:00 1.144315245 0.000694098 0.000694098 1.537338501 -0.315794574    24.88113695 40978.64755
01-04-2015 10:30:00 1.007904412 0.000608322 0.000608322 1.444485294 0.714613971     19.82169118 32870.0591
01-04-2015 11:00:00 1.044117647 0.000630179 0.000630179 1.425086505 -0.058088235    28.92387543 47932.57617
1
It would be better if you dput a few lines of the first few columns of the dataset to make sure that it works with the types of objects you're working with. - A5C1D2H2I1M1N2O1R2T1
Please use dput. As in dput(head(yourdataset, 4)). - A5C1D2H2I1M1N2O1R2T1

1 Answers

3
votes

You can do something like this:

df <- data.frame(timestamp = 1:2, A = 3:4, B = 5:6, C = 7:8)

lapply(2:ncol(df), function(x) df[, c(1, x)])

This would create a list of the smaller data.frames that you are after.

If you really wanted separate objects in your workspace, you would need to try something like:

list2env(
  setNames(
    lapply(2:ncol(df), function(x) df[, c(1, x)]), 
    paste0("df", 1:(ncol(df)-1))), 
  .GlobalEnv)

(But it seems really messy to me to have so many separate objects in your workspace.) list2env needs names, which is why we first add them to the list of split data.frames using setNames.

EDIT: Changed the code from df[c(1, x)] to df[, c(1, x)] since you are working with xts objects and not data.frames.