split a data.table in R by key -


i have data.table object in r split along keys.

>mytable[1:11]    id     length  hash 1: 2578   52.5   26566273 2: 4066   52.5   26566273 3: 2578   53.5   26566273 4: 4066   53.5   26566273 5: 2207   29.5   54352910 6: 3719   29.5   54352910 7: 5166    9.5  613353882 8: 5167    9.5  613353882 9: 5169    9.5  613353882 10:5170    9.5  613353882 11:5171    9.5  613353882 

first_hash , length 2 keys output list each key of id column

so looks like

first_hash length id_list 26566273   52.5   [1] 2578 4066 26566273   53.5   [1] 2578 4066 54352910   29.5   [1] 2207 3719 613353882   9.5   [1] 5166 5167 5168 5169 5170 5171 

or kind of list...

i think plyr can give answers this, prefer data.table way

the ultimate goal create pairs of id having same key aware of function expand.grid

thanks

we group 'hash' , 'length' , place 'id' in list.

dt <-  mytable[,list(id_list=list(id)) , =.(first_hash=hash, length)] dt #   first_hash length                  id_list #1:   26566273   52.5                2578,4066 #2:   26566273   53.5                2578,4066 #3:   54352910   29.5                2207,3719 #4:  613353882    9.5 5166,5167,5169,5170,5171  str(dt) # classes ‘data.table’ , 'data.frame':  4 obs. of  3 variables: # $ first_hash: int  26566273 26566273 54352910 613353882 # $ length    : num  52.5 53.5 29.5 9.5 # $ id_list   :list of 4 # ..$ : int  2578 4066 # ..$ : int  2578 4066 # ..$ : int  2207 3719 # ..$ : int  5166 5167 5169 5170 5171 

or @frank mentioned, can paste 'id' create column group instead of list

 mytable[,list(id_list= tostring(id)) , =.(first_hash=hash, length)] 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -