“Not in” in R Post date 2011/10/04 Post categories In code, r, rblogs When processing data it is common to test if an observation belongs to a set. Let’s suppose that we want to see if the sample code belongs to a set that includes A, B, C and D. In R it is easy to write something like: [sourcecode language=”r”] inside.set = subset(my.data, code %in% c(‘A’, ‘B’, ‘C’, ‘D’)) [/sourcecode] Now, what happens if what we want are the observations that are not in that set? Simple, we use the negation operator (!) as in: [sourcecode language=”r”] outside.set = subset(my.data, !(code %in% c(‘A’, ‘B’, ‘C’, ‘D’))) [/sourcecode] In summary, surround the condition by !(). Post navigation ←Previous post:A shoebox for data analysis→Next post:All combinations of levels for two factors Leave a Reply Cancel replyComment Name * Email * Website