I have a large dataframe in R composed of 4 columns and thousands of lines. Here are the first lines as an example:
ID V1 V2 Stimulus
[1,]
Firstly, create the new colum (dat
is the name of your dataset):
Status <- ave(dat[ , "Stimulus"], c(0, cumsum(abs(diff(dat[ , "Stimulus"])))),
FUN = function(x)
if(!x[1]) "PRE" else c(rep("OK", min(2, length(x))),
rep("POST", length(x) - 2)))
Now, combine both objects:
cbind(dat, Status)
The result:
ID V1 V2 Stimulus Status
[1,] 1 74.80 803.0 0 PRE
[2,] 1 75.98 790.9 0 PRE
[3,] 1 75.95 791.1 0 PRE
[4,] 1 65.70 918.7 0 PRE
[5,] 1 59.63 1005.6 13 OK
[6,] 1 59.44 1012.0 13 OK
[7,] 1 59.62 1010.0 13 POST
[8,] 1 63.85 942.4 13 POST
[9,] 1 60.75 992.9 0 PRE
[10,] 1 59.62 1010.0 0 PRE
[11,] 1 61.68 974.0 0 PRE
[12,] 1 65.21 921.4 15 OK
[13,] 1 59.23 1012.0 15 OK
[14,] 1 61.23 979.5 15 POST
[15,] 1 70.80 849.2 0 PRE