0

I am working with a large dataset that contains variables measured from plots. These plots are continuously remeasured every couple of years. The data sort of looks like the table below. Now I want to look at growth after a period of time so I want to select the variables (tph and basal area) initial measurements at Time1 and final measurement at Time2 of each plot. But I am not sure how to do it. Should I run a for loop or a foreach loop? or is there any other function or package in R that deals with data like this? And, what would the script look like?

State County Plot Measured_year basal_area tph
1     1      1    2006          10         10 
2     1      2    2007          20         20
1     1      1    2009          30         30 
2     1      1    2005          40         40
2     1      1    2010          50         50
2     1      2    2013          60         60
sakar299
  • 19
  • 3
  • Does this answer your question? [Summarize data within multiple groups of a time series](https://stackoverflow.com/questions/56728060/summarize-data-within-multiple-groups-of-a-time-series) – Michael Roswell Jun 14 '22 at 14:20

1 Answers1

1

If I understood your question correctly, I think you could consider the following approach :

library(dplyr)
State <- c(1, 2, 1, 2, 2, 2)
County <- c(1, 1, 1, 1, 1, 1)
Plot <- c(1, 2, 1, 1, 1, 2)
Measured_year <- c(2006, 2007, 2009, 2005, 2010, 2013)
basal_area <- c(10, 20, 30, 40, 50, 60)
tph <- c(10, 20, 30, 40, 50, 60)

df <- data.frame(State, County, Plot, Measured_year, basal_area, tph)

df_Time1 <- df %>% group_by(Plot) %>%  slice(which.min(Measured_year)) 
df_Time2 <- df %>% group_by(Plot) %>%  slice(which.max(Measured_year)) 

df_Total <- bind_rows(df_Time1, df_Time2)
df_Total[df_Total$Plot == 1, ]
Emmanuel Hamel
  • 1,769
  • 7
  • 19