0

I have a univariate data frame:

Y=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] 

I would like to split it so that I obtain different data frames for x timesteps. For eg, for the case of 2 timesteps, I'd like:

Y[1]=[1,2]
Y[2]=[3,4]
Y[3]=[5,6]

and so on.....

I even have python code for the same -

dfs = list()
for x in range(0, len(Y), 2):
    df = Y.iloc[x:x+2]
    df.columns= ['one','two']
    dfs.append(df)
for df in dfs:
    print(df)
    print()
DGT
  • 39
  • 9
  • 3
    I hope this helps you out: https://stackoverflow.com/questions/3318333/split-a-vector-into-chunks-in-r – Carles Feb 08 '19 at 18:41
  • variateY <- matrix(Y, ncol = 2, nrow = dim(Y)[1]/2, byrow = TRUE) – Rex Feb 08 '19 at 18:54
  • @CarlesSansFuentes 'split' definitely is a good idea, however I want a new dataframe for each split. Any ideas on that? – DGT Feb 08 '19 at 18:57
  • @Rex, there is an error:Error in matrix(Y, ncol = 2, nrow = dim(Y)[1]/2, byrow = TRUE) : invalid 'nrow' value (too large or NA) – DGT Feb 08 '19 at 18:58
  • Sorry about that. `y` is a vector so it has only one dimension. Use `matrix(Y, nrow = length(Y)/2, ncol = 2, byrow = TRUE)` – Rex Feb 08 '19 at 19:25
  • @Rex, this is perfect !!! Thanks a lot. – DGT Feb 08 '19 at 19:29
  • Glad I could help! – Rex Feb 08 '19 at 19:30

0 Answers0