r - Can't split dataframe into equal buckets preserving order without introducing Xn. prefix -
i trying split ordered data frame 10 equal buckets. following works introduces x1., x2., x3. ... prefix each bucket, prevents me iterating on buckets sum them.
num_dfs <- 10 buckets<-split(df, rep(1:num_dfs, each = round(nrow(df) / num_dfs)))
produces df[10] looks like:
$`10` predicted_duration actual_duration 177188 23.7402944 6 466561 23.7402663 12 479556 23.7401721 5 147585 23.7401666 48
here's crude code using try sum groups.
for (i in c(1,2,3,4,5,6,7,8,9,10)){ p<-sum(as.data.frame(df[i],row.names=null)$x1.actual_duration) # x1., x2., print(paste(i,"=",p)) }
how remove xn.
grouping prefix or programmatically reference using index i
?
here's similar reproducible example:
df<-data.frame(actual_duration=sample(100)) num_dfs <- 10 df_grouped<-as.data.frame(split(df, rep(1:num_dfs, each = round(nrow(df) / num_dfs)))) (i in c(1,2,3,4,5,6,7,8,9,10)){ p<-sum(df[i]$actual_duration) # not work because postfix .1, .2.. added r print(paste(p)) }
i'm not entirely clear on issue is, if trying sum group couldn't use
library(tidyverse) df <- data.frame(actual_duration=sample(100)) df %>% arrange(actual_duration) %>% mutate(group = rep(1:10, each = 10)) %>% group_by(group) %>% summarise(sums = sum(actual_duration))
alternatively if want keep list format
df %>% arrange(actual_duration) %>% mutate(group = factor(rep(1:10, each = 10))) %>% split(., .$group) %>% map(., function(x) sum(x$actual_duration))
Comments
Post a Comment