Ram
Ram

Reputation: 331

Split dataframe based on one column in r

I have a huge dataframe of around 1M rows and want to split the dataframe based on one column & different ranges. Example dataframe:

    length<-sample(rep(1:400),100)
    var1<-rnorm(1:100)
    var2<-sample(rep(letters[1:25],4))
    test<-data.frame(length,var1,var2)

I want to split the dataframe based on length at different ranges (ex: all rows for length between 1 and 50).

    range_length<-list(1:50,51:100,101:150,151:200,201:250,251:300,301:350,351:400)

I can do this by subsetting from the dataframe, ex: test1<-test[test$length>1 &test$length<50,] But i am looking for more efficient way using "split" (just a line)

Upvotes: 1

Views: 1860

Answers (1)

eddi
eddi

Reputation: 49448

range = seq(0,400,50)
split(test, cut(test$length, range))

But do heed Justin's suggestion and look into using data.table instead of data.frame and I'll also add that it's very unlikely that you actually need to split the data.frame/table.

Upvotes: 2

Related Questions