r - Make a number of dataframes by using loop -


i have data frame "df" following:

df=data.frame(time=as.factor(rep(c(0.01*(1:100)),each=49)),           roi=rep(1:49,100),           area=runif(4900, 5.0, 7.5),hours=(rep(1:10,each=490))) 

the df split 10 smaller data frames based on column "hours"

x=split(df,df$hours) 

in each sub-data frame 1 new data frame "br[i] made following:

br1=data.frame(x$`1`[c(1,round(nrow(x$`1`)*(1:4)/4)),]) br1$min=c(0,15,30,45,60) br2=data.frame(x$`2`[c(1,round(nrow(x$`2`)*(1:4)/4)),]) br2$min=c(0,15,30,45,60) ... br10=data.frame(x$`10`[c(1,round(nrow(x$`10`)*(1:4)/4)),]) br10$min=c(0,15,30,45,60) 

the question how made 10 data frames "br" automatically without repeat such above commands in many time?

thanks response!

try:

lapply(split(df,df$hours), function(x) {     br=data.frame(x[c(1,round(nrow(x)*(1:4)/4)),])     br$min=c(0,15,30,45,60)     return(br) }) 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -