r - how can i take out the first two rows of each subject ID -


I have data that looks like I can take data from the first two rows of each subject ID I want to reduce How can I do this for all people?

  Understanding ID time 1 0 1 1 5 5 1 7 11 2 0 0.5 2 1 10 2 2 15  

Thanks in advance!

You can:

  library (dplyr) library ( Tidyr) Df% & gt;% group_by (id)% & gt;% piece (1: 2) # id time CONC # 1 1 0 1.0 # 2 1 5 5.0 # 3 2 0 0.5 # 4 2 1 10.0  

or data.table

  library (data.table) setDT (df) [, SD [1: 2], = ID From] # id time CONC # 1: 1 0 1.0 # 2: 1 5 5.0 # 3: 2 0 0.5 # 4: 2 1 10.0  

or

 < Code> set DT (DF) [DF [, I Using [1: 2], id = $ 1],  

or base r

  df [with ( DF, AEE (seq_along (ID), ID, FUN = seq_along) <3),] #from @ John's comments  

In this case, you reduce the code given above ( id is not a factor variable.

  df [with (df, ave (id, id, fune = seq_along) & Lt; 3),]  

data
  df <- structure (list (id = c (1l), 1l, 1 L, 2L, 2L, 2L), time = C (0L, 5L, 7L, 0L, 1L, 2L), CONC = c (1, 5, 11, 0.5, 10, 15 )) .name = c ("ID", "TIME", "CONC"), class = "data .fra Me ", line.Name = C (NA, -6L))  


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -