library(tidyverse)
library(data.table)
DF = read.csv("data.csv")
Assuming the “wide” data have the same type of observation at each time point listed as a set of variables. These variables should have consistent names such as “Time_1”, “Time_2”,…,”Time-N” or “Month1”,”Month2”,…,”MonthN” etc.
DFlong = DF %>% pivot_longer(cols = starts_with("Time_"),
names_to="Time",names_prefix ="Time_",
values_to= "ValueName",values_drop_na=FALSE)
DF = data.table(DF)
DF[, ID := ID[1], .(cumsum(!is.na(ID)))]
library(data.table)
library(runner)
DF = data.table(DF)
DF = DF %>% group_by(ID) %>% mutate(Var = runner::fill_run(Var, run_for_first = T))
DF = DF %>% group_by(ID) %>% mutate(NewSeq = seq_len(n()))
DF = DF%>% group_by(ID) %>% mutate(Day = dense_rank(Date))
library(nlme) ## trellis plot method
GroupedDF <- groupedData(ref~Day|ID, data=DF, inner=~Group)
plot(GroupedDF, aspect=3)
or
ggplot(DF, aes(x, y, group =ID))+geom_line(aes(col=ID),alpha=0.5) ## ggplot method
D1 = DFlong %>% group_by(ID) %>% summarise(NewVar = min(Var))
D1 = DFlong %>% group_by(ID) %>% summarise(NewVar = max(Var))
D1 = DFlong %>% group_by(ID) %>% summarise(NewVar = Var[1]))
D1 = DFlong %>% group_by(ID) %>% summarise(NewVar = min(which(Var!= Value)))
XSectional=DFlong%>%distinct(Var1, Var1, Var3, Var4)