0

So I have a data frame and I want to get every 11 rows. Not just the every 11th rows but a chunk of 11 rows every time for eg:

     Subject Wt Dose  Time  conc
1        1 79.6 4.02  0.00  0.74
2        1 79.6 4.02  0.25  2.84
3        1 79.6 4.02  0.57  6.57
4        1 79.6 4.02  1.12 10.50
5        1 79.6 4.02  2.02  9.66
6        1 79.6 4.02  3.82  8.58
7        1 79.6 4.02  5.10  8.36
8        1 79.6 4.02  7.03  7.47
9        1 79.6 4.02  9.05  6.89
10       1 79.6 4.02 12.12  5.94
11       1 79.6 4.02 24.37  3.28

and then later 11 and then again the other following 11 rows.

I tried this

for (i in 1:nrow(Theoph)) {
  everyEleven = Theoph[11,i]
  everyEl
}

But it just gives me the first 11 rows and not the second chunk of 11 rows and so on

Liviosah
  • 325
  • 2
  • 11
  • So you want a list of dataframes, each one of 11 rows? – Ric S Apr 23 '20 at 12:30
  • Yea I want every 11 rows, because different subjects are in every 11 rows.For eg.: subject 1 is in the first 11 and then subject 2 is in the next 11 and so on. Its because I want to make a plot that is time against concentration for every subject. – Liviosah Apr 23 '20 at 12:42
  • 2
    Maybe better to assign a subject ID. `Theoph$ID <- rep (1:m, each=11)` where `m` is the number of subjects. Is it 12 by any chance? – Edward Apr 23 '20 at 12:59
  • I am not sure if I understand the question completely, but I interpret you want 11 rows per group according to a grouping column. Unclear is: Which 11 rows (the first last, random...)? My first answer would use a `data.table`: `library(data.table); x <- data.table(mtcars); x[, head(.SD, 2), by = gear]` for the first 2 rows per group (column "gear") – R Yoda Apr 23 '20 at 13:09

5 Answers5

2

Maybe you can try split like below

everyEleven <- split(Theoph,ceiling(seq(nrow(Theoph))/11))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
1

Try this as adapted from [split into multiple subset of dataframes with dplyr:group_by?

library(tibble)
library(dplyr)  
library(tidyr)

Make an indicative dataframe as your data in the question is only 11 rows.

tib <- tibble(sub = rep(1:33),
              var = runif(33))

tib1 <- 
  tib %>% 
  # create a grouping variable every 11 rows , unless there is a variable in your data which does the same. 
  mutate(grp = rep(1:3, each = 11)) %>% 
  group_by(grp) %>%
  nest()%>%
  select(data) %>%
  unlist(recursive = FALSE)


Gives you:

$data1
# A tibble: 11 x 2
     sub   var
   <int> <dbl>
 1     1 0.258
 2     1 0.337
 3     1 0.463
 4     1 0.856
 5     1 0.466
 6     1 0.701
 7     1 0.548
 8     1 0.999
 9     1 0.454
10     1 0.292
11     1 0.173

$data2
# A tibble: 11 x 2
     sub   var
   <int> <dbl>
 1     2 0.148
 2     2 0.487
 3     2 0.246
 4     2 0.279
 5     2 0.130
 6     2 0.730
 7     2 0.312
 8     2 0.935
 9     2 0.968
10     2 0.745
11     2 0.485

$data3
# A tibble: 11 x 2
     sub        var
   <int>      <dbl>
 1     3 0.141     
 2     3 0.200     
 3     3 0.00000392
 4     3 0.993     
 5     3 0.644     
 6     3 0.334     
 7     3 0.567     
 8     3 0.817     
 9     3 0.0342    
10     3 0.718     
11     3 0.527   

Peter
  • 11,500
  • 5
  • 21
  • 31
1

Since in the sample data you provided there is a column Subject, which I assume represents the subject IDs and there are only 11 rows with the same value for Subject, you can use

split(Theoph, Theoph$Subject)
Ric S
  • 9,073
  • 3
  • 25
  • 51
0

I will assume your data frame is 11*N rows long then

everyEleven = vector(mode = "list", length = N)

for(i in 1:N){
  start = (i - 1) * 11 + 1
  end = i * 11
  everyEleven[[i]] = Theoph[start:end, ]
}
James Curran
  • 1,274
  • 7
  • 23
0

We can use gl to create the grouping index

split(Theoph, as.integer(gl(nrow(Theoph), 11, nrow(Theoph))))
akrun
  • 874,273
  • 37
  • 540
  • 662