Hello R Users, I'm wondering if there exists any elegant and simple way to do the following: I have a data.frame X fills in numbers. I have a vector y with numbers as well. Every value in X that is equal to any values in y should be replaced by e.g. 1. Any idea? Robert
Robert Ruser <robert.ruser <at> gmail.com> writes:> > Hello R Users, > I'm wondering if there exists any elegant and simple way to do the > following: I have a data.frame X fills in numbers. I have a vector y with > numbers as well. Every value in X that is equal to any values in y should > be replaced by e.g. 1. Any idea? > > Robert >If the data frames are completely filled with numbers you should be able to operate on them as matrices (?as.matrix, ?as.data.frame). X <- matrix(1:16,nrow=4) Y <- matrix(rep(2:5,4),nrow=4) (X[X %in% c(Y)] <- 1)
On 11/06/2010 11:36 AM, Robert Ruser wrote:> Hello R Users, > I'm wondering if there exists any elegant and simple way to do the > following: I have a data.frame X fills in numbers. I have a vector y with > numbers as well. Every value in X that is equal to any values in y should > be replaced by e.g. 1. Any idea?Keep in mind FAQ 7.31 when programming this...