Dear all, I would like to subset a dataframe using multiple conditions. So if I have two columns 1 and 2, I would like to EXCLUDE all rows in which the value of column 1 is "a" AND the value of column 2 is "b". I tried data[(data$column1 != "a" & data$column2 != "b"),] but that deletes me every row in which either 1 is "a" or 2 is "b". Any ideas? Thanks so much K [[alternative HTML version deleted]]
Hello, It's difficult to know what's going on without a data example. Can you post the output of dput(head(data, 30)) ? Maybe the columns are not of class "character" but of class "factor". You should also post the output of str(data) In the mean time, try subset(data, column1 != "a" & column2 != "b") Hope this helps, Rui Barradas Em 09-05-2013 17:28, KatrinH Heimann escreveu:> Dear all, > I would like to subset a dataframe using multiple conditions. > So if I have two columns 1 and 2, I would like to EXCLUDE all rows in which > the value of column 1 is "a" AND the value of column 2 is "b". > I tried data[(data$column1 != "a" & data$column2 != "b"),] but that deletes > me every row in which either 1 is "a" or 2 is "b". > Any ideas? > Thanks so much > K > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Hi, On Thu, May 9, 2013 at 12:28 PM, KatrinH Heimann <katrinheimann at gmail.com> wrote:> Dear all, > I would like to subset a dataframe using multiple conditions. > So if I have two columns 1 and 2, I would like to EXCLUDE all rows in which > the value of column 1 is "a" AND the value of column 2 is "b". > I tried data[(data$column1 != "a" & data$column2 != "b"),] but that deletes > me every row in which either 1 is "a" or 2 is "b".Close: you have the negation in the wrong place:> fakedata <- data.frame(x1 = sample(letters[1:3], 10, replace=TRUE), x2 = sample(letters[1:3], 10, replace=TRUE), stringsAsFactors=FALSE) > fakedatax1 x2 1 c c 2 a b 3 a a 4 c a 5 c a 6 c a 7 a b 8 c c 9 b a 10 c a> subset(fakedata, !(x1 == "a" & x2 == "b"))x1 x2 1 c c 3 a a 4 c a 5 c a 6 c a 8 c c 9 b a 10 c a -- Sarah Goslee http://www.functionaldiversity.org