user9629272
user9629272

Reputation:

How to create a subset by using another subset as condition?

I want to create a subset using another subset as a condition. I can't show my actual data, but I can show an example that deals with the core of my problem.

For example, I have 10 subjects with 10 observations each. So an example of my data would be to create a simple data frame using this:

    ID <- rep(1:10, each = 10)
    x <- rnorm(100)
    y <- rnorm(100)
    df <- data.frame(ID,x,y)

Which creates:

       ID           x           y
   1    1  0.08146318  0.26682668
   2    1 -0.18236757 -1.01868755
   3    1 -0.96322876  0.09565239
   4    1 -0.64841436  0.09202456
   5    1 -1.15244873 -0.38668929
   6    1  0.28748521 -0.80816416
   7    1 -0.64243912  0.69403155
   8    1  0.84882350 -1.48618271
   9    1 -1.56619331 -1.30379070
   10   1 -0.29069417  1.47436411
  11   2 -0.77974847  1.25704185
  12   2 -1.54139896  1.25146126
  13   2 -0.76082748  0.22607239
  14   2 -0.07839719  1.94448322
  15   2 -1.53020374 -2.08779769 
    etc.

Some of these subjects were positive for an event (for example subject 3, 5 and 7), so I have created a subset for that using:

    event_pos <- subset(df, ID %in% c("3","5","7"))

Now, I also want to create a subset for the subjects who were negative for an event. I could use something like this:

   event_neg <- subset(df, ID %in% c("1","2","4","6","8","9","10"))

The problem is, my data set is too large to specify all the individuals of the negative group. Is there a way to use my subset event_pos to get all the subjects with negative events in one subset?

TL;DR

Can I get a subset_2 by removing the subset_1 from the data frame?

Upvotes: 0

Views: 55

Answers (1)

Guillaume Ottavianoni
Guillaume Ottavianoni

Reputation: 496

You can use :

ind_list <- c("3","5","7")

event_neg <- subset(df, (ID %in% ind_list) == FALSE)

or

event_neg <- subset(df, !(ID %in% ind_list))

Hope that will helps

Gottaviannoni

Upvotes: 1

Related Questions