1

I have a data frame with columns called "row," "col," and "time". Row has values from A through H and Col has values from 1 through 12. Time has values of "5, 10, 15, 20, 25, 30."

So I want to make different data frames or sets of vectors that contain the same number of row & col but different times. So in the end, there would be 96 different data frames or sets of vectors that have matching row and col but different values in time and activity.

Below is a sample of my data set.

     row col time activity day
1     A   1    5       33   1
2     B   1    5       36   1
3     C   1    5       53   1
4     D   1    5       40   1
5     E   1    5       91   1
6     F   1    5       80   1
7     G   1    5       89   1
8     H   1    5       82   1
97    A   1   10       38   1
98    B   1   10       92   1  
99    C   1   10       47   1
100   D   1   10       57   1
101   E   1   10       84   1
102   F   1   10       85   1
103   G   1   10       96   1

Below is a sample of what I would like…

     row col time activity day
1     A   1    5       33   1
2     A   1   10       38   1
3     A   1   15       66   1

etc etc

Thank you~

Jaap
  • 81,064
  • 34
  • 182
  • 193
hj14
  • 169
  • 5
  • 12
  • Please see some USEFUL approaches [here](http://stackoverflow.com/questions/22232009/error-using-select-function-in-r/22232450#22232450) and, more exhaustively, [here](http://stackoverflow.com/questions/5805271/subset-a-data-frame-based-on-column-entry-or-rank/5820329#5820329) – Paulo E. Cardoso Mar 06 '14 at 21:26
  • @PauloCardoso, I'm not sure those posts address the issue here, though I could well be misunderstanding the OP. See my answer for my interpretation. – BrodieG Mar 06 '14 at 21:36
  • I think the discussion [here](http://stackoverflow.com/q/3505701/892313) is more relevant; in this case hj14 is just asking about the split part of split-apply-combine. However, I suspect that that is just a prelude to applying and combining and so the entire corpus there is relevant. If not, the relevant parts can be pulled out. – Brian Diggs Mar 06 '14 at 21:43
  • @BrodieG True. I misunderstood. Your approach bellow may be what is being asked. – Paulo E. Cardoso Mar 06 '14 at 21:44

1 Answers1

2

If I understood correctly, you want to split your data frame into sub data frames where each sub data frame has the same rows and Col values. You can do this with split:

df.split <- with(df, split(df, list(rows, Col)))
head(df.split, 3) # showing 3 of 96 sub data frames

Produces

$A.1
    rows Col time activity
1      A   1    5       26
97     A   1   10       91
193    A   1   15       25
289    A   1   20        2
385    A   1   25       95
481    A   1   30       35

$B.1
    rows Col time activity
2      B   1    5       64
98     B   1   10       71
194    B   1   15       72
290    B   1   20       45
386    B   1   25       52
482    B   1   30       43

$C.1
    rows Col time activity
3      C   1    5       49
99     C   1   10       27
195    C   1   15       35
291    C   1   20       16
387    C   1   25        9
483    C   1   30       94

And here is the toy data I used:

rows <- LETTERS[1:8]
Col <- 1:12
time <- (1:6) * 5  
df <- expand.grid(rows=rows, Col=Col, time=time)
df$activity <- sample(1:100, nrow(df), rep=T)
BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • This works great. Thank you. I was wondering how I could name each graph produced with the name of each data frame. So like the first graph with the name "A.1" and second with "B.1" etc – hj14 Mar 06 '14 at 21:56
  • @hj14, It isn't clear to me if you are still wondering how to name the graphs, or whether that is already answered for you. Additionally, if this answers your original question, please consider marking it as answered. Thanks. – BrodieG Mar 06 '14 at 22:02
  • @hj14, that will depend on how you're doing your plotting. Depending on what you are doing you may not want to split the data at all. You should ask that as a separate question. I also recommend you `dput` a small version of your data (maybe only 2 times, and two rows, and two Cols) to make it easier for people to help out. – BrodieG Mar 06 '14 at 22:43