-1

Hi I have the following data frame (Unt):

    Day       sunrise   sunset   day.length

1    Monday     6.42    19.97       13.55
2    Monday     6.42    19.97       13.55
3    Monday     6.42    19.97       13.55
4    Monday     6.42    19.97       13.55
5    Monday     6.42    19.97       13.55
6    Monday     6.42    19.97       13.55
7    Monday     6.42    19.97       13.55
8    Friday     6.42    19.97       13.55
9    Friday     6.42    19.97       13.55
10   Friday     6.42    19.97       13.55
11   Saturday       6.42    19.97       13.55
12   Saturday       6.42    19.97       13.55
13   Saturday       6.42    19.97       13.55
14   Saturday       6.42    19.97       13.55
15   Saturday       6.42    19.97       13.55
16   Sunday     6.42    19.97       13.55
17   Sunday     6.42    19.97       13.55
18   Sunday     6.42    19.97       13.55
19   Sunday     6.42    19.97       13.55

I am trying to remove the rows with days "Sunday" and "Saturday". I've tried the following

    week<-subset(Unt, Day!="Sunday" & Day!="Saturday")

    week<- Unt [!Unt$Day %in% c("Sunday", "Saturday"), ]

However in neither case the rows were removed.

Roland
  • 127,288
  • 10
  • 191
  • 288
Vasile
  • 1,017
  • 2
  • 10
  • 19
  • 2
    Make sure there are no space in your Day variable before you try to move rows. Delete space gsub('^\\s+|\\s+$','',Unt$Day) – Shenglin Chen Jun 24 '15 at 17:39
  • 2
    Its the same person-The OP couldn't solve his problem hence the reposting. – John_dydx Jun 24 '15 at 17:42
  • What is the output of `str(Unt$Day)` and `unique(Unt$Day)`? – Roland Jun 24 '15 at 17:44
  • Please post the output of `dput(Unt)` as your code appears to work with the data as you posted it above. – Sam Firke Jun 24 '15 at 17:45
  • 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
  • Both `subset(Unt, Day!="Sunday" & Day!="Saturday")` and `Unt [!Unt$Day %in% c("Sunday", "Saturday"), ]` works perfectly fine on your data set. – David Arenburg Jun 24 '15 at 17:53
  • 1
    From the previous question, it looks like the OP has " Saturday" and " Sunday" in the data frame - the leading spaces need to be included in the subset or removed wile reading in data. – jeremycg Jun 24 '15 at 18:05
  • 2
    @Vasile Reproducible data has a pretty narrow meaning (here's that link again: http://stackoverflow.com/a/28481250/1191259 ). It means, roughly, that we can copy-paste something from your question into R and look at the exact same data you're seeing. In R, `dput` is the thing for that. – Frank Jun 24 '15 at 18:06
  • 1
    @ everyone else, @jeremycg : I recommended posting a new question (see my comment there) since that question was answered and yet the OP was still running into a problem. I think this is better than having those answerers update their answers as the question changes. – Frank Jun 24 '15 at 18:07
  • Thanks everyone for support. The problem with my data set was indeed related to spaces. I saw it when run str(), but after it was pointed by you. Thanks Frank, for dput. Will surely use it next time. – Vasile Jun 24 '15 at 19:17
  • 1
    I flagged as a duplicate, but the actual issue was a typo, not how to filter out rows. I doubt this will be useful to future readers as is, and it may simply waste the time of those who stumble onto it. Should this question be closed? Same goes for the other post with this question. – Sam Firke Jun 24 '15 at 20:21
  • 1
    @SamFirke Yeah, I agree and VTC this as a typo. The other question seems more appropriately marked as a dupe (as you have already marked it), not a typo. – Frank Jun 24 '15 at 22:27

2 Answers2

2

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

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

Unt[!(Unt$Day %in% c(" Saturday", " Sunday")), ]

Or remove them while reading in the csv.

Community
  • 1
  • 1
jeremycg
  • 24,657
  • 5
  • 63
  • 74
1

Here's a possible solution to your question

day.length <- c(rep(13.55, 19))
Day <- factor(c(rep("Monday", 7), rep("Friday", 3),
rep("Saturday", 5), rep("Sunday", 4)))
sunset <- c(rep(6.42, 19))
sunrise <- c(rep(19.97, 19))

df <- data.frame(Day, sunrise, sunset, day.length )
df2 <- subset(df, Day!= "Sunday" & Day!= "Saturday")

df2 is what you want. As pointed out by jeremycg, you should remove any spaces from your Day variables.

OP, perhaps you could try adding strip.white = TRUE as in data <- read.csv("data.csv", header = TRUE, strip.white = TRUE) when you upload the data into R and see if this solves the problem? Alternatively, you could remove the white space after loading the data-see this post for more details. Otherwise, it will be very difficult to help out without having access to the original dataset that's creating the problem.

Community
  • 1
  • 1
John_dydx
  • 951
  • 1
  • 14
  • 27