1

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 is split into 10 smaller data frames based on column "hours"

x=split(df,df$hours)

In each sub-data frame one new data frame "br[i] is 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 is how to made 10 data frames "br" automatically without repeat such above commands in many time?

Thanks so much for any response!

1
  • 1
    perhaps lapply(x, function(x) x[c(1,round(nrow(x)*(1:4)/4)),]) Commented Aug 8, 2016 at 13:00

1 Answer 1

4

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)
})
Sign up to request clarification or add additional context in comments.

1 Comment

@ Abdou: Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.