Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个data.frame,d:
d<-data.frame(id=c(1,1,1,1,2,2,3,3,3,3), var=c("no", "no", "no", "yes", "no", "yes", "no", "yes", "yes", "yes"))
我想为每个导致“是”的 ID 返回所有“否”行,包括第一个“是”。
期望的结果:
id var 1 no 1 no 1 no 1 yes 2 no 2 yes 3 no 3 yes
正如@joran 所指出的,包含您尝试过的内容是有礼貌的。它还增加了您的问题获得投票的可能性。这个问题足够基本,即使是对 R 的初步理解也可以得出一些结论。
无论如何,这将做你想要的:
d[! duplicated(d) | d$var == 'no', ]