For each value, myvector1
I would like to know the value mycategory
for the immediate previous value in myvector1
, given that it is mystatus
on; otherwise, I would look for the corresponding next value as long as it is on.
The instructions look like this:
- For this "myvector" position, give me your value.
- Find the position of the intermediate previous value in "Myvector"
- Check asssociated status. If it is on, give me "My_category". If it is turned off, go to step 2 again.
- Assign the resulting "mycategory" to the new "mysolution" vector.
Given the dataset mydf
I'm looking for DesiredSolution
(which I filled out manually).
mydf <- structure(list(myvector1 = structure(c(1L, 2L, 3L, 4L, 5L, 1L,
2L, 4L, 5L, 2L, 3L, 4L, 5L, 2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 4L, 5L, 1L, 1L, 2L, 3L, 4L, 5L, 3L), .Label = c("0", "1",
"2", "3", "4"), class = "factor"), mystatus = structure(c(2L,
1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("OFF",
"ON"), class = "factor"), mycategory = structure(c(2L, 2L, 3L,
1L, 1L, 1L, 1L, 3L, 3L, 1L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L), .Label = c("bye",
"hi", "stay"), class = "factor"), DesiredSolution = structure(c(3L,
3L, 3L, 3L, 3L, 2L, 3L, 1L, 3L, 1L, 4L, 4L, 4L, 1L, 2L, 4L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L), .Label = c("bye",
"hi", "NA", "stay"), class = "factor")), .Names = c("myvector1",
"mystatus", "mycategory", "DesiredSolution"), row.names = c(NA,
-32L), class = "data.frame")