0

one of the columns in my data frame -z- contains weekdays. I would like to remove rows that have "Sunday" and "Saturday" as factor. I used without success r<-z[-c(z$x=="Sunday" & z$x=="Saturday", ] and r<-subset(z, x!=="Sunday" & x!=="Saturday"). x is the name of the column.

Could you please help figure it out? Thanks. The data:

y   x
1   1   Monday
2   2   Monday
3   3   Monday
4   4   Monday
5   5   Monday
6   6   Monday
7   7   Monday
8   8   Friday
9   9   Friday
10  10  Friday
11  11  Friday
12  12  Sunday
13  13  Sunday
14  14  Sunday
15  15  Sunday
16  16  Saturday
17  17  Saturday
18  18  Saturday
19  19  Saturday
20  20  Saturday

UPDATE: I accepted the answer as it worked with the df I provided. When I try to apply it to the df I have it does not do anything. I use flexW<-subset(flex, Day!="Sunday" & Day!="Saturday")

The selected variable from the data frame:

str(flex) 'data.frame': 177237 obs. of 14 variables: .... $ Day : Factor w/ 7 levels " Friday"," Monday",..: 2 2 2 2 2 2 2 2 2 2 ...

Vasile
  • 1,017
  • 2
  • 10
  • 19
  • Please give a reproducible example using a data that's accessible to everyone-e.g. `mtcars` data in R. I'll have a go and get back to you. – John_dydx Jun 24 '15 at 16:04
  • 1
    Your code would be fine **but** you need to understand that this is being tested per row/observation. There's no way that x for a row can be both "Sunday" and "Saturday". What you mean to test is whether it's Sunday or Saturday. The (first couple) answers below obscure this fact because `x == "Sunday" | x == "Saturday"` can be more concisely written as `x %in% c("Saturday","Sunday")`. Also, minor thing: the `-` to negate only works if you're evaluating row numbers. For logical, you'll want `!` – Frank Jun 24 '15 at 16:10
  • 2
    And, in your second try, `!==` isn't valid R, `!=` is what you want. – Gregor Thomas Jun 24 '15 at 16:12
  • 1
    Thanks for your explanations. The answer below worked well and I can understand now better why. – Vasile Jun 24 '15 at 16:19
  • Your post is likely to get less attention now that your original question has been answered. You might consider starting a new question with "reproducible" data http://stackoverflow.com/a/28481250/1191259 – Frank Jun 24 '15 at 16:33
  • Thanks, I will wait for a while and if nothing comes up will ask a new question. – Vasile Jun 24 '15 at 16:37
  • possible duplicate of [Filtering a data frame in R](http://stackoverflow.com/questions/1686569/filtering-a-data-frame-in-r) – Sam Firke Jun 24 '15 at 17:47

5 Answers5

3

following your example, try this:

subset(z, x!="Sunday" & x!="Saturday")
John_dydx
  • 951
  • 1
  • 14
  • 27
  • I accepted the answer as it worked with the df I provided. When I try to apply it to the df I have it does not do anything. – Vasile Jun 24 '15 at 16:27
  • Please see the update to the question. It does not give any error, but apparently saves the same data frame. – Vasile Jun 24 '15 at 16:35
  • It definitely works when I run it on R-are you sure you're saving it as a different object? – John_dydx Jun 24 '15 at 16:39
  • try running this and tell me what it gives you `str(flexW)`. – John_dydx Jun 24 '15 at 16:41
  • John, I just created a reproducible dataset. Tried on it with the same result. I will follow the advice to ask a new question as it is easier for me to insert all the data. Thanks – Vasile Jun 24 '15 at 17:01
2

Just a base R solution:

dat[!dat$x %in% c("Sunday", "Saturday"), ]
    y      x
1   1 Monday
2   2 Monday
3   3 Monday
4   4 Monday
5   5 Monday
6   6 Monday
7   7 Monday
8   8 Friday
9   9 Friday
10 10 Friday
11 11 Friday

or one with dplyr, almost the same syntax.

dat %>% filter(!x %in% c("Sunday", "Saturday"))
SabDeM
  • 7,050
  • 2
  • 25
  • 38
  • 1
    this is easier to read than big conditional statements jammed in a one liner – Aaron Katch Jun 24 '15 at 16:16
  • 1
    @AaronKatch Personally, I find `!x` very misleading/hard to read. Needs more parens and/or better spacing to show what is being negated: `!( x %in% c("a","b") )` – Frank Jun 24 '15 at 16:28
  • 1
    @Frank your code style is better, thank you for this hint. – SabDeM Jun 24 '15 at 16:31
2
> x[x$X != 'Saturday' & x$X!= 'Sunday',]
    y  x      X
1   1  1 Monday
2   2  2 Monday
3   3  3 Monday
4   4  4 Monday
5   5  5 Monday
6   6  6 Monday
7   7  7 Monday
8   8  8 Friday
9   9  9 Friday
10 10 10 Friday
11 11 11 Friday
bhavin
  • 122
  • 7
1

From your error output, you have " Friday"," Monday" as inputs. The leading spaces are being stripped as people here try and reproduce, you need to use dput(flex) rather than paste so things like this don't happen.

I guess your saturday and sunday columns are the same, so try:

 z[!(z$x %in% c(" Saturday", " Sunday")), ]
jeremycg
  • 24,657
  • 5
  • 63
  • 74
0

Maybe this could work for you:

 z[!z$x %in% c("Saturday", "Sunday"), ]
erasmortg
  • 3,246
  • 1
  • 17
  • 34