[英]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. 我有一个大约100万行的巨大数据框,并希望根据一列和不同范围拆分该数据框。 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). 我想根据长度在不同范围内分割数据帧(例如:长度在1到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) 我可以通过从数据帧进行子设置来做到这一点,例如:
test1<-test[test$length>1 &test$length<50,]
但是我正在寻找使用“ split”(仅一行)的更有效方法
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. 但是请注意贾斯汀的建议,并考虑使用
data.table
而不是data.frame
,我还要补充一点,您实际上不太可能需要拆分data.frame / table。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.