1

I have a data frame structured like this:

>data
  ID  Location Sex   Time  Var1  Var2  Var3  Var4  Var5  Var6
   12     A     M     .1    …      …     …     …     …     … 
   12     A     M     .2    …      …     …     …     …     … 
   12     A     M     .3    …      …     …     …     …     … 
   12     A     M     .4    …      …     …     …     …     …
   12     A     M     .5    …      …     …     …     …     … 
   12     A     M     .6    …      …     …     …     …     … 
   234    A     M     .1    …      …     …     …     …     … 
   234    A     M     .2    …      …     …     …     …     … 
   234    A     M     .3    …      …     …     …     …     … 
   234    A     M     .4    …      …     …     …     …     … 
   234    A     M     .5    …      …     …     …     …     …  

There are several hundred individuals, each having a unique ID number. The data for each individual is time series data. In the real data, there are several hundred measurements per individual. For instance in the example above there are only 6 observations for individual 12, and 5 observations for individual 234. In reality individual 12 may have 980 observations and individual 234 may have 1249 observations.

I can create a data frame that contains the first row (first observation) for each individual like this:

library(tidyverse)
FirstPt<-
data%>%
group_by(ID)%>%
slice(1)

Then I can use pivot_longer() to re-arrange the data like I need it:

FirstPt<-
FirstPt%>%
pivot_longer(MgCa:PbCa, names_to = "Variable")

I want to do this same thing for the last observation on each individual, but how do I do so when they are all of different lengths?

In addition, how could I combine the 'pivot_longer()' command into FirstPt<-data%>%group_by(ID)%>%slice(1) so that it would all be accomplished in one chunk?

Ryan
  • 1,048
  • 7
  • 14
  • 1
    ***Note:*** I mentioned the links to the gold-badger and asked them to close the question with multiple dupes. They have not answered to the question and then closed it by themselves. – M-- Jan 16 '20 at 21:54

1 Answers1

1

We can use n() instead of 1 for the last observation

library(dplyr)
library(tidyr)
data %>%
  group_by(ID)%>%
  slice(n()) %>%
  pivot_longer(cols =  MgCa:PbCa, names_to = "Variable")

Or with filter

data %>%
  group_by(ID) %>%
  filter(row_number() == n())
akrun
  • 874,273
  • 37
  • 540
  • 662