s-news
[Top] [All Lists]

subsetting a data.frame by a list

To: <s-news@wubios.wustl.edu>
Subject: subsetting a data.frame by a list
From: "Boylan, Richard" <rboylan@bus.olemiss.edu>
Date: Thu, 29 Nov 2001 16:24:21 -0600
Thread-index: AcF5JHhr2/AegePwEdWO8QABAxxsSA==
Thread-topic: subsetting a data.frame by a list

This is a simple question that I could not find good keywords to search
through the archives.

Let
data <- data.frame( x = c(1,2,4,4,3,5,6), y = c(1,2,3,4,5,6,7) )

What I would like to do is change the values of y for values of x that
belong to some list.

So, for instance,
data$y[ data$x == 1 | data$x == 4] <- 3
will change the values of y to 3 if x is 1 or 4.

I would like to be able to do that for a long list of values of x, so adding
all these | is not proctical.

So, what I have in mind is something like
list.x <- list(1,4)
data$y[ data$x in list.x] <- 3.

Of course there is no such command, so I was wondering how does one
do that.


Richard Boylan
Croft Associate Professor of International Economics
University of Mississippi

<Prev in Thread] Current Thread [Next in Thread>
  • subsetting a data.frame by a list, Boylan, Richard <=