本文介绍了拆分数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个data.table,我想分成两个。我这样做如下:I have a data.table which I want to split into two. I do this as follows:dt <- data.table(a=c(1,2,3,3),b=c(1,1,2,2))sdt <- split(dt,dt$b==2)但是如果我想下一步做这样的事情but if I want to to something like this as a next stepsdt[[1]][,c:=.N,by=a]想知道是否有更好的方法拆分表,以便它会更有效(并且不会得到这个消息)?Just wondering if there is a better way of splitting the table so that it would be more efficient (and would not get this message)?推荐答案 p>这在v1.8.7中工作(也可以在v1.8.6中工作):This works in v1.8.7 (and may work in v1.8.6 too) :> sdt = lapply(split(1:nrow(dt), dt$b==2), function(x)dt[x])> sdt$`FALSE` a b1: 1 12: 2 1$`TRUE` a b1: 3 22: 3 2> sdt[[1]][,c:=.N,by=a] # now no warning> sdt$`FALSE` a b c1: 1 1 12: 2 1 1$`TRUE` a b1: 3 22: 3 2但是,就像@mnel说的效率低。如有可能,请避免分割。But, as @mnel said, that's inefficient. Please avoid splitting if possible. 这篇关于拆分数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 18:40