0

I was looking into a data frame in which there are multiple user ids, many of them repeating e.g. in the following format

 V1    V2                   V3       V4
 1002  2015-09-05 12:38:55  2953644  998999024421701
 1002  2015-09-03 22:42:08  8495424  998999025009405
 1004  2015-09-08 01:36:30  1498309  998999024420383
 1005  2015-09-07 13:44:58  517720   998999024419011

Now I'm used to key-value pairs (being from a Java background) so the closest we get here are lists. On reading this, I tried working on a peculiar request.

Basically I want that for each unique id (V1 = 1002), I should be able to print all the rows associated with that id so the output would be something like this:

  1002
  2015-09-05 12:38:55  2953644  998999024421701
  2015-09-03 22:42:08  8495424  998999025009405
  1004
  2015-09-08 01:36:30  1498309  998999024420383
  1005
  2015-09-07 13:44:58  517720   998999024419011

I tried using split as in here

 xy.list <- setNames(split(g1, seq(nrow(g1))),g1$V)

but it won't club the results.

demongolem
  • 9,474
  • 36
  • 90
  • 105
hbabbar
  • 947
  • 4
  • 15
  • 33

1 Answers1

0

This will split your data.frame into a list of data.frame

xy.list<-split(g1,g1$V1)

Note: the split will work correctly even if V1 is not sorted in any order.
You can then use it to index by name. Say to extract id=1002:

xy.list[['1002']]
fishtank
  • 3,718
  • 1
  • 14
  • 16
  • I tried doing that but by doing this it returns me only one row associated with this ID so e.g. if we use xy.list[['1002']] it will essentially return me only one row and not two as i expect – hbabbar Jan 14 '16 at 03:49
  • It should work correctly. My guess is that you forgot to quote it. i.e. `xy.list[[1002]]` is different from `xy.list[['1002']]`. – fishtank Jan 15 '16 at 18:30