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?