Recode id in different time spans

I have longitudinal data on the same individuals across different time spans. How do I assign a new id to the same individual when the time is reset. Here is an example dataset: df <- data.frame(time=c(1,2,3,4,2,3,4,5,6,3,4), id=c(1,1,1,1,1,1,1,1,1,1,1)) df time id 1 1 2 1 3 1 4 1 2 1 3 1 4 1 5… Read More Recode id in different time spans

how to create a count variable that counts based on the value of a binary variable in R using dplyr

code to create df: I have the repeat data that looks like the table below. df <- structure(list(patid = c("1", "1", "1", "1", "2", "2", "3", "3", "3", "4", "4", "4", "4"), observation_date = c("07/07/2016", "07/08/2016", "07/11/2016", "07/07/2019", "07/05/2015", "02/12/2016", "07/05/2015", "07/06/2015", "16/06/2015", "07/05/2015", "02/12/2016", "18/12/2016", "15/01/2017"), registration = c("0","0","1","1","0","1","0","0","0","0","1","1","1")), class = "data.frame", row.names =… Read More how to create a count variable that counts based on the value of a binary variable in R using dplyr

R: Randomly Replace Values with NA

I am working the R programming language. I am trying select 10% of the elements in my dataset (excluding elements in the first column) and replace them with NA. I tried to do this with the following code: library(longitudinalData) data(artificialLongData) second_dataset = artificialLongData second_dataset[sample(nrow(second_dataset),0.1*nrow(second_dataset ))]<- NA This produces the following error: Error in `[<-.data.frame`(`*tmp*`, sample(nrow(second_dataset),… Read More R: Randomly Replace Values with NA