根据r中的一列拆分数据框 [英] Split dataframe based on one column in r

查看:1377
本文介绍了根据r中的一列拆分数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大约1M行的巨大数据帧,并希望基于一列分割数据帧。不同范围。
示例数据框:

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)

我可以通过数据框中的子集来执行此操作,例如: test1< test但是我正在寻找更有效的方法,使用split(只是一行)

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)

推荐答案

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

但是请注意Justin的建议,并研究使用 data.table 而不是 data.frame ,我还将补充说,您实际上不太可能需要来拆分数据。框架/表格。

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.

这篇关于根据r中的一列拆分数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆