I need in 2nd column a count of how many times observation is in 1st column. Here, 2nd column should have values 1,2,3,1,2,1,2. This code doesn't work.
x <- c(11,11,11,22,22,33,33) y <- c(1,1,1,1,1,1,1) df <- data.frame(x,y)
i <- 1 for (i in 1:(nrow(df)-1)){
if(df[i+1,1] == df[i,1]){df[i+1,2] <- 2}
if(df[i+2,1] == df[i,1]){df[i+2,2 <- 3}
else df[i+2,2] <- 1}
Aucun commentaire:
Enregistrer un commentaire