r - Reorganising a data frame -


i have data frame 1 column , 158112 different values. values not ordered randomly. every 24 values represent 1 day. every day listed in there 18 times , followed next day, eg. 18x24 01.01.2012, 18x24 02.01.2012 , on.

        df 1       593 2       939 3       734 4       791 5       184 6       495 ... 158112  683 

i want organise them in new data frame in different structure. process kind of this:

take first 24 values , put them new data frame "new_df" column no. 1, take next 24 values , put "new_df" column no. 2, take next 24 values put "new_df" column no. 3. until 18 columns filled each 24 values , start again column no.1 , add next 24 values , on... @ end have "new_df" 18 columns , 8784 rows each.

any ideas?

i think want following:

# sample data mydf <- data.frame(df=rnorm(18*8784,0,1)) # split dataframe chunks (of 18*24) mylist <- split(mydf,rep(1:366,each=432)) # turn each chunk matrix of right shape , `rbind` them new_df <- do.call(rbind, lapply(mylist, function(x) matrix(x[,1],nrow=24))) 

you can check if right with:

all.equal(mydf[1:24,1],new_df[1:24,1]) # first 24 values first column all.equal(mydf[25:48,1],new_df[1:24,2]) # next 24 values second column all.equal(mydf[433:456,1],new_df[25:48,1]) # day 2 starts in first column 

all of should true. , guess want data.frame, use as.data.frame(new_df) result data.frame.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -