Question

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~

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top