I have a data frame with only one column and 158112 different values. The values are not ordered randomly. Every 24 values represent one day. Every day is listed in there 18 times and the followed by the next day, eg. 18x24 for the 01.01.2012, 18x24 for the 02.01.2012 and so on.
df
1 593
2 939
3 734
4 791
5 184
6 495
...
158112 683
I want to organise them in a new data frame in a different structure. The process would kind of look like this:
Take the first 24 values and put them into the new data frame "new_df" column no. 1, take the next 24 values and put the into "new_df" column no. 2, take the next 24 values an put the into "new_df" column no. 3. Do this until 18 columns are filled with each 24 values and then start again with column no.1 and add the next 24 values and so on... So at the end I would like to have the "new_df" with 18 columns and 8784 rows each.
Any ideas?
I think you want something like the following:
# sample data
mydf <- data.frame(df=rnorm(18*8784,0,1))
# split dataframe into chunks (of 18*24)
mylist <- split(mydf,rep(1:366,each=432))
# turn each chunk into a matrix of the right shape and `rbind` them back together
new_df <- do.call(rbind, lapply(mylist, function(x) matrix(x[,1],nrow=24)))
You can check if this is right with:
all.equal(mydf[1:24,1],new_df[1:24,1]) # first 24 values are first column
all.equal(mydf[25:48,1],new_df[1:24,2]) # next 24 values are second column
all.equal(mydf[433:456,1],new_df[25:48,1]) # day 2 starts in the first column
All of those should be TRUE
. And I guess you want it as a data.frame, so just use as.data.frame(new_df)
to get the result back into a data.frame.