Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 208897

Mutate based on conditional lag

$
0
0

I have data from an IM chat, where each row is a keystroke. I would like to automatically add the column most_recent_enter (manually added here, for an explanation), where it tracks the most recent row where the keystroke == ENTER.

In this example, there are overlapping messages, so the last user to hit ENTER was not necessarily the most recent user. I am including other columns here, to show what information I have available.

x <- data.frame(overall_idx = 1:14,
                sender=c("a","a","a","b","b","b","a",
                         "a","a","a","b","b","a","a"),
                keystroke=c("H","I","ENTER","K","I","ENTER",
                            "W","H","I","C","W","H","H","T"),
                ks_idx=c(0,1,2,0,1,2,0,1,2,3,0,1,3,3),
                most_recent_enter=c(NA,NA,NA,"a","a","a","b","b",
                                    "b","b","b","b","b","b")
                )

Is there a way to find the most recent row that meets a condition?


Viewing all articles
Browse latest Browse all 208897

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>