Home > Back-end >  Calculated field based on string value of another column R
Calculated field based on string value of another column R

Time:09-03

I have a dataframe with a column "status" and session

enter image description hereenter image description here

My question is how can I create a for loop that populates the field session with the numbers 1-20 based on the START or STOP value in the other column?

So my result would give something like the following

Status Session START 1 STOP 1 START 2 STOP 2 START 3 STOP 3

CodePudding user response:

Let DF be your data frame.

## solution 1
DF$Session <- rep(1:(nrow(DF) / 2), each = 2)

## solution 2
DF$Session <- cumsum(DF$Status == "START")
  • Related